Using jq or alternative command line tools to compare JSON files

If your shell supports process substitution (Bash-style follows, see docs): diff <(jq –sort-keys . A.json) <(jq –sort-keys . B.json) Objects key order will be ignored, but array order will still matter. It is possible to work-around that, if desired, by sorting array values in some other way, or making them set-like (e.g. [“foo”, “bar”] → … Read more

How to merge 2 JSON objects from 2 files using jq?

Since 1.4 this is now possible with the * operator. When given two objects, it will merge them recursively. For example, jq -s ‘.[0] * .[1]’ file1 file2 Important: Note the -s (–slurp) flag, which puts files in the same array. Would get you: { “value1”: 200, “timestamp”: 1382461861, “value”: { “aaa”: { “value1”: “v1”, … Read more

How to install JQ on Mac on the command line?

You can install any application/packages with brew on mac. If you want to know the exact command just search your package on https://brewinstall.org and you will get the set of commands needed to install that package. First open terminal and install brew ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” < /dev/null 2> /dev/null Now Install jq brew … Read more

Passing bash variable to jq

Consider also passing in the shell variable (EMAILID) as a jq variable (here also EMAILID, for the sake of illustration): projectID=$(jq -r –arg EMAILID “$EMAILID” ‘ .resource[] | select(.username==$EMAILID) | .id’ file.json) Postscript For the record, another possibility would be to use jq’s env function for accessing environment variables. For example, consider this sequence of … Read more