Compare Json Files in Beyond Compare
Asked Answered
P

3

13

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.

Pyrites answered 30/3, 2020 at 2:52 Comment(0)
P
7

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

Pyrites answered 30/3, 2020 at 2:52 Comment(1)
This is great, but it is even better if you add , sort_keys=True to the json.dump call (that is json.dump(data, file_w, indent=4, sort_keys=True)). This way, you won't get differences because of the JSON object key order, which is arbitrary.Helprin
A
11

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

Archipelago answered 23/1, 2021 at 2:26 Comment(5)
This didn't work for me. The other answer is better.Helprin
there are 3 additional files. JSON, JSON tidied and JSON sorted. for me JSON tidied works best for most case. but that depends on what your comparing. I did install all 3.Ierna
It only works for me if I use the "Open File" dialog. Pasting JSON content in the left or right pane doesn't reformat it.Prolific
@AurélienGasser When pasting text, it doesn't know it's JSON and you'll have to pick "JSON Tidied" from the Format menu. Unfortunately, the conversion is implemented with files instead of pipes, so it'll ask you to save to files. Another option is pbpaste | jq . | pbcopy to tidy JSON in the clipboard, but you'll have to acquire those utilities on Windows.Archipelago
Cheers this was super helpful to me. I set the tab stops to 1 and it made my JSON much more manageableHypotrachelium
P
7

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

Pyrites answered 30/3, 2020 at 2:52 Comment(1)
This is great, but it is even better if you add , sort_keys=True to the json.dump call (that is json.dump(data, file_w, indent=4, sort_keys=True)). This way, you won't get differences because of the JSON object key order, which is arbitrary.Helprin
R
1

I found the JSON tidied additional file format quite good. As it is able to prettify the JSON during the comparison.

enter image description here

Resentment answered 29/9, 2023 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.