jqwinmerge

Sorting and comparing json files with ./jq


I am trying to compare two long json files with WinMerge by using QueryJSON plugin. Properties in those files are in random order. That parameter for .\jq does work, but nested objects are not affected:

QueryJSON ". | to_entries | sort_by(.key) | from_entries"

There is some "flatten" command which did not provide me result that I searched for (may be used incorrectrly).

Jsons could be for example:

{
   "Id": 1,
   "Addresses":[
      {"Street": "Street 1",
      "PostalCode": 12345},
      {"Street": "Street 2",
      "PostalCode": 12345}
   ],
   "Person":{
      "FirstName": "Mike",
      "LastName":"Boe"
   }
}


{
   "Id": 2,
   "Person":{
      "FirstName": "John",
      "LastName":"Boe"
   }
   "Addresses":[
      {"Street": "Street 3",
      "PostalCode": 12345},
      {"Street": "Street 2",
      "PostalCode": 12345}
   ]
}

I can compare nested objects one by one by pointing at them like

QueryJSON ".Person | to_entries | sort_by(.key) | from_entries"

But is there any way that will sort properties also inside these objects (even if that would flatten them)? Thanks.


Solution

  • jq does have an option that sorts all the keys inside the objects:

    --sort-keys / -S:
    
            Output the fields of each object with the keys in sorted order.
    

    So you can just use the following command:

    jq --sort-keys .
    

    Or, in WinMerge syntax:

    QueryJSON --sort-keys "."