
Why jsdp (Json Sort in Deep)?
jsdp is a simple tool for recursively sorting objects and arrays in JSON files.
It is useful in situations where you want to compare JSON files that have the same content but different order.
While jq -S and dictknife can sort objects, they cannot sort arrays.
In such cases, jsdp is superior as it can also sort the contents of arrays.
Install
go
go install github.com/izziiyt/jsdp
mise
mise use --global go:github.com/izziiyt/jsdp
Example
(jq is required)
$ cat 0.json
{
"b": 2,
"a": [
false,
{
"a": 1
},
1,
null,
2.1,
"value",
[3, 2, 1]
]
}
$ cat 1.json
{
"a": [
2.1,
1,
{
"a": 1
},
null,
[2, 3, 1],
"value"
],
"b": 2
}
$ diff <(jsdp 0.json | jq) <(cat 1.json | jsdp | jq)
3d2
< false,
Sorting Order
Everything is sorted in ascending order.
The data types in the JSON are described according to RFC-8259.
object
Objects are sorted by the string order of their keys.
before
{
"c": false,
"a": 1,
"b": null
}
after
{
"a": 1,
"b": null,
"c": false
}
array
Arrays are sorted first by the following order of data types:
- false
- true
- number
- string
- array
- object
- null
Subarrays divided by type are then sorted according to their respective types.
before
{
"a": [
{ "a": 1},
false,
"b",
1,
null,
"a",
[1, false],
true,
0.5
]
}
after
{
"a": [
false,
true,
0.5,
1,
"a",
"b",
[false, 1],
{ "a": 1},
null
]
}