3

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?

  • Possible duplicate of [Using jq or alternative command line tools to compare JSON files](https://stackoverflow.com/questions/31930041/using-jq-or-alternative-command-line-tools-to-compare-json-files) – Jakob Apr 28 '19 at 20:57

1 Answers1

3

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 ...

peak
  • 105,803
  • 17
  • 152
  • 177