Verify a json node is present

Is there any custom method which can check if a json node is present?

I have a json like this

{
“body”: {
“user”: {
“surname”: “Morris”,
“firstName”: “James”,
“dateOfBirth”: “1983-04-19”,
“titleCode”: “Mr”,
“middleName”: “P”,
“genderCode”: “male”
}
}
}

I do see method like

WS.verifyElementPropertyValue(response, 'body.user.firstName', "James")
WS.verifyElementsCount(response, 'body.user', 6)

which are fine. Is there any method to check if a specific node is present using katalon inbuilt method, something like

WS.verifyNodePresent(response, 'body.user.firstName', true)  
WS.verifyElementsCount(response, 'body.user.firstName', 1)

I don’t want to use something like

response.body.user.surname != null

I hope if Katalon can expose some api’s like i suggested, code will be more consistent. Or else i will end up with some custom methods and some katalon specific methods. The reason for choosing Katalon is the rich set of API’s they provide. I am sure this will be the requirement for most of the teams which verify the json body.

Thank you

1 Like

Well, that’s a shame because using Groovy’s GPath methodology for both JSON and XML would give you exactly that capability – and it’s pretty easy. But if you don’t want to use it, well… I guess you’ll need to wait until someone adds the APIs you want.

Thank you Russ Thomas for the reply and I apologize if i was bit rude.

I agree with you regarding GPath or JsonPath. Currently i am using something like that. Most of the cases there will be some other alternative to do something.

What i meant was, if Katalon exposes api for checking whether a node is present or not, code will be consistent[Can always use Katalon API’s] and more over anyone can use it. Even if there coding skills are limited. I believe that is the ultimate aim of Katalon.

Already Katalon support methods like WS.verifyElementPropertyValue(response, ‘body.user.firstName’, “James”) for checking a node value. So i feel like, if you tweak it a little bit, that specific functionality can be achieved.

Katalon is a great tool with lots of features and is provided for free. Thanks for that.

1 Like

I didn’t think you were rude. I thought you made your case quite clearly (as did @devalex88, it seems - one of the developers).

You could add these methods yourself. Groovy makes it relatively easy to add new methods (Keywords in Katalon).

https://docs.katalon.com/katalon-studio/docs/introduction-to-custom-keywords.html

@Mathew.Kuruvila

The right method to check for node presence (and json structure in general) is to use schema validation.
See Justify, a JSON Schema Validator, in Katalon Studio

other option:

Parse the json with JsonSlurper. Based on the json response (array or object) the parsed object will be a List or a Map. So, use groovy native methods to check if a given value or key is present.
Ref. docs:

https://www.tutorialspoint.com/groovy/groovy_lists_contains.htm

https://www.tutorialspoint.com/groovy/groovy_containskey.htm

This will work for XML too (just use XMLSlurper instead)

Sure, i will check those

Thanks