Stéphane GRILLON
Stéphane GRILLON

Reputation: 11862

Karate - How to parse response contain a name space in json?

My Karate feature for response:

* def response = response
* print 'response is: ', response 

console:

   {
      "dd:PrepareMasterBookingRS": {
        "@Version": "1.27",
        "@TransactionID": "123456798",
        "dd:Success": {
        },
        "dd:AvailabilityResponse": {
          "dd:RoomStays": {
            "dd:RoomStay": [
              {
                "@RPH": "1",
                "dd:RoomTypes": {
                  "dd:RoomType": [
                    {
                      "@NumberOfUnits": "18",
                      "@RoomTypeCode": "R1123",
                      "dd:RoomDescription": {
                        "@Name": "C12N - vvvvvv 1-2 pers. 1 foo"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }

My Karate feature for response:

* def roomStay = response.dd:PrepareMasterBookingRS.dd:AvailabilityResponse.dd:RoomStays.dd:RoomStay[0];

My console error:

org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:11 Expected ; but found :
response.dd:PrepareMasterBookingRS.dd:AvailabilityResponse.dd:RoomStays.dd:RoomStay[0];
           ^

- org.graalvm.polyglot.Context.eval(Context.java:401)
- com.intuit.karate.graal.JsEngine.evalForValue(JsEngine.java:141)
- com.intuit.karate.graal.JsEngine.eval(JsEngine.java:137)
- com.intuit.karate.core.ScenarioEngine.evalJs(ScenarioEngine.java:1252)
- com.intuit.karate.core.ScenarioEngine.evalKarateExpression(ScenarioEngine.java:2205)
- com.intuit.karate.core.ScenarioEngine.evalKarateExpression(ScenarioEngine.java:2124)
- com.intuit.karate.core.ScenarioEngine.evalAndCastTo(ScenarioEngine.java:1316)

Upvotes: 1

Views: 164

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Special characters in JSON keys have to handled differently. Try this:

* def roomstay = response['dd:PrepareMasterBookingRS']['dd:AvailabilityResponse']['dd:RoomStays']['dd:RoomStay'][0]

Also note that you can try things like this:

* def roomstay = $response..['dd:RoomStay']

Upvotes: 1

Related Questions