简体   繁体   中英

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 $aa json string) with no luck. Any idea how to approach strings, not files?

It would probably be simplest to use the --argjson command-line option, eg

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

Of course there are alternatives, eg using jq -s ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM