Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq to diff json in bash

Tags:

json

diff

jq

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?

like image 220
Simon Ernesto Cardenas Zarate Avatar asked Dec 04 '25 12:12

Simon Ernesto Cardenas Zarate


1 Answers

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

like image 165
peak Avatar answered Dec 07 '25 12:12

peak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!