Skip to content

Python script that sorts a JSON object according to its keys. Useful for diff'ing two JSON files.

License

Notifications You must be signed in to change notification settings

JElchison/sort-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

sort-json

Python script that sorts a JSON object according to its keys.

The product of this script is deterministic, so this is useful for diff'ing two JSON files.

Example Usage

Sort a JSON file:

user@computer:~$ cat abc.json 
{
    "c": 0,
    "b": 0,
    "a": {
      "a3": 1,
      "a2": 2,
      "a1": 3
    }
}
user@computer:~$ ./sort-json.py abc.json 
{
    "a": {
        "a1": 3,
        "a2": 2,
        "a3": 1
    },
    "b": 0,
    "c": 0
}

Diff two JSON files:

user@computer:~$ cat abcd.json 
{
    "d": 0,
    "c": 0,
    "b": 0,
    "a": {
      "a3": 1,
      "a2": 2,
      "a1": 3
    }
}
user@computer:~$ diff <(./sort-json.py abc.json) <(./sort-json.py abcd.json)
8c8,9
<     "c": 0
---
>     "c": 0,
>     "d": 0

References

About

Python script that sorts a JSON object according to its keys. Useful for diff'ing two JSON files.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages