How to parse JSON responses in Katalon Studio


#1

This is a companion discussion topic for the original entry at https://docs.katalon.com/katalon-studio/tutorials/parse_json_responses.html

#2

Hi,
I am trying to get json result like this parsedJson.get(“FeeDefinition”).get(“Agent”).get(“Percentage”) but when I use the second get() It gives me a syntax error and when I run in debug mode then I get this message

groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.get() is applicable for argument types: (java.lang.String) values: [Agent]
Possible solutions: get(int), get(int), getAt(java.lang.String), getAt(java.lang.String), grep(), grep()

My JSON is:
> {

      "FeeDefinition": [
        {
          "Constraint": {
            "Floor": 10.0,
            "Ceiling": 10000000.0
          },
          "Agent": {
            "Percentage": 0.0,
            "Fixed": 0.0
          },
          "Sender": {
            "Percentage": 0.0,
            "Fixed": 0.0
          },
          "Receiver": {
            "Percentage": 0.0,
            "Fixed": 0.0
          }
        }
      ],
      "Config": {
        "ValidateOnLoad": false,
        "ServiceDescription": "Safaricom"
      }
    }

#3

Hi Ajmal,

FeeDefinition is an array with one object, so you must select array element and then Agent object.

parsedJson.get(“FeeDefinition”).get(0).get(“Agent”).get(“Percentage”)


#4

Thanks for pointing out the issue <3


#5

Had an issue where the keys in the json array contained ‘.’ (period) characters. The JSON parsing groks on this as it assumes you’re using array/object sublevels

The workaround is to use the fact that JS is happy to treat objects as arrayable.

So

{
  "status":400,
  "message":"The given data was invalid.",
  "errors":{
    "to.0":"The to.0 must be a valid email address.",
    "to.1":"The to.1 must be a valid email address."
  }
}

can be checked using

WS.verifyElementPropertyValue(response, 'errors["to.0"]', 'The to.0 must be a valid email address.')
WS.verifyElementPropertyValue(response, 'errors["to.1"]', 'The to.1 must be a valid email address.')