jq to diff json in bash

I have 2 json objects comming from a rest api. I want to compare if they are the same object. objectA:

{
  "type": {
    "S": "equal"
  },
  "preFilter": {
    "BOOL": true
  }
}

objectB:

{
  "preFilter": {
    "BOOL": true
  },
  "type": {
    "S": "equal"
  }
}

They are the same, but an md5sum will see them as different. I tried inserting them in 2 different files, and compare the files using something proposed here: but I would like to know if it's possible to use jq on the fly to compare variables.

I've been trying to change

--argfile a a.json

for

--arg a $a

(being $a a json string) with no luck. Any idea how to approach strings, not files?

Upvotes: 3

Views: 6405

Answers (1)

peak
peak

Reputation: 116710

It would probably be simplest to use the --argjson command-line option, e.g.

jq -n --argjson a "$a" --argjson b "$b" '$a == $b'

Of course there are alternatives, e.g. using jq -s ...

Upvotes: 3

Related Questions