Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Json Files in Beyond Compare

How can I compare two minified json files in beyond compare? Is there a built in file format for json? I'm looking to compare two pretty print representations of the underlying json objects.

like image 921
jxramos Avatar asked Mar 02 '23 15:03

jxramos


2 Answers

In this thread a representative says:

While not in the box yet, we do have a JSON sorted format available for download in our Additional File Formats section:

With a link to Scooter Software Downloads

like image 92
Tim Sylvester Avatar answered Apr 28 '23 01:04

Tim Sylvester


You can achieve this specialized diff functionality by defining a new file format conversion rule in beyond compare. This example was conducted in the Windows OS.

Step 0: Create a python conversion script to render the formatted json. Save the following python script somewhere on your harddrive

import json
import sys

sourceFile = sys.argv[1]
targetFile = sys.argv[2]

with open(sourceFile, 'r') as file_r:
    # Load json data
    data = json.load(file_r)

    # Write formatted json data
    with open(targetFile, 'w') as file_w:
        json.dump(data, file_w, indent=4)

Step 1: Navigate in the BeyondCompare menu to: Tools-->File Formats...

Step 2: Create new file format entry by clicking on the + button and select Text Format New text file format

Step 3: Enter *.json into the file format's Mask field, and any description that will help you recall the file format's purpose. Define new file format

Step 4: Define the file format's conversion settings. Select the Conversion tab and select External program (unicode filenames) from the pull down. In the Loading field write the following shell command

python C:\Source\jsonPrettyPrint.py "%s" "%t"

Conversion settings for file format

Step 5: Press the Save button and optionally rename the file format by right clicking it in the File Formats Name and Mask table.

Further specializations of the json dumping could be considered by looking at the python documentation, eg sort_keys=True

like image 34
jxramos Avatar answered Apr 27 '23 23:04

jxramos