0

I am implementing a ready api project in which I have to compare two JSON objects.
Say Obj1 = {"A":"Test1","B:"Test2"} is an input.

I have a regular expression inside the script file in which I have
Obj = '''{"A":".*","B":".*"}''' and I tried to do assert obj1 == obj which didn't work.

Could some one tell me if script file in ReadyAPI doesn't support regular expression in such format?

halfer
  • 19,824
  • 17
  • 99
  • 186
ChanChow
  • 1,346
  • 7
  • 28
  • 57
  • So, all you need to do is matching the keys alone as you are not bothered about values? Is your real json is just that simple or more has complexity ? – Rao Oct 07 '17 at 01:45
  • ChanChow, please see below solution to see if that helps. – Rao Oct 07 '17 at 03:52

1 Answers1

1

With your example, you can do the comparison by parsing using JsonSlurper and get the keys before comparing as you are not bothered about the values.

See the example script:

def obj1 = """{
  "A" : "Test1",
  "B" : "Test2"
}"""

def obj2 = """{
  "A" : "Test1a",
  "B" : "Test2b"
}"""

def getJsonKeys = { jsonString ->
   def json = new groovy.json.JsonSlurper().parseText(jsonString)
   json.keySet()
}

assert getJsonKeys(obj1) == getJsonKeys(obj2)

Note that there are different values for the keys in obj2. But, just compared only keys.

Also note that if your json has more depth, you may to have to alter solution based on the data. Assuming that you have given right data.

You can quickly try it online demo

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Hi Rao, thanks for the response. It gives me some idea how to progress forward. I have a indepth json structure as {"A": "Test1", "B" : {"B1":"TestB1","B2","TestB2","B3":[{"B31":"TestB31","B32":"TestB32"}]}. Could you plz provide me some approach – ChanChow Oct 09 '17 at 22:15
  • Have you tried the demo? if helpful appreciate up vote :). If you have deep, then see if [this](https://stackoverflow.com/questions/39973668/recursively-extracting-json-field-values-in-groovy) post is helpful. – Rao Oct 10 '17 at 00:32