Looking for a Custom Keyword that should return json value based on the jsonpath supplied as parameter

@Katalon_Store @katalon_store_suppor

Looking for a Custom Keyword that should return json value based on the jsonpath supplied as parameter.

System.err.println CustomKeywords.‘className.getJsonValue’(‘definitions.address.properties.state.type’)
System.err.println CustomKeywords.‘className.getJsonValue’(‘properties.shipping_address.allOf[1].properties.type.enum[0]’)

@Keyword
public static String getJsonValue(String jsonPath){

  def jsonValue;
  def slurper = new groovy.json.JsonSlurper()

  def result = slurper.parseText('''

{
“$schema”: “http://json-schema.org/draft-06/schema#”,

“definitions”: {
“address”: {
“type”: “object”,
“properties”: {
“street_address”: { “type”: “string” },
“city”: { “type”: “string” },
“state”: { “type”: “string” }
},
“required”: [“street_address”, “city”, “state”]
}
},

“type”: “object”,

“properties”: {
“billing_address”: { “$ref”: “#/definitions/address” },
“shipping_address”: {
“allOf”: [
{ “$ref”: “#/definitions/address” },
{ “properties”:
{ “type”: { “enum”: [ “residential”, “business” ] } },
“required”: [“type”]
}
]
}
}
}
‘’')

return result.jsonPath()

}

You can’t pass a string for this.

At the call site, you know the path you need (because you’re passing it as a string). Instead, pass a Closure and write the steps needed to pull the result out using a GPath.

http://docs.groovy-lang.org/latest/html/documentation/core-semantics.html#gpath_expressions