J. Doem
J. Doem

Reputation: 649

compare json irrespective of field value in groovy

I have a JSON file that has same fields with different values. Now I have to write a groovy script that compares the two files which will ignore fields value.

e.g

json1 = '{"name" : "abc", "value": "123", "field" : "xyz"}'
json2 = '{"name" : "efg", "value": "567", "field" : "xyz"}'

assert should return true

json1 = '{"value": "123", "field" : "xyz"}'
json2 = '{"name" : "efg", "value": "567", "field" : "xyz"}'

assert should return false

I have try with the following code (from here) and always return false for both case

def slurp1 = new JsonSlurper().parseText(json1)
def slurp2 = new JsonSlurper().parseText(json2)

assert slurp1 == slurp2

Upvotes: 1

Views: 701

Answers (1)

tim_yates
tim_yates

Reputation: 171054

Can't you just do

slurp1.keySet() == slurp2.keySet()

Upvotes: 3

Related Questions