0

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
J. Doem
  • 619
  • 7
  • 21

1 Answers1

3

Can't you just do

slurp1.keySet() == slurp2.keySet()
tim_yates
  • 167,322
  • 27
  • 342
  • 338