Francesco F.
Francesco F.

Reputation: 89

access a json field value if the json field has special chars as dots

If I have a json file with fields having special chars (in my case dots) how can I access the field value in Karate?

For example having a json file called example.json

{
  "field1" : {
      "field2" : "value2",
      "field.3" : "value3"
  }
}

if I want to get the value of "field.3" field how can do?

  Scenario: read a json file
    * def myJson = read("example.json")
    * match myJson.field1.field2 == "value2"
    * match myJson.field1.field.3 == "value3" # this fails
    * match myJson.field1."field.3" == "value3" # this fails
    * match myJson.field1.'field.3' == "value3" # this fails
    * match myJson.field1.'field\.3' == "value3" # this fails

Upvotes: 1

Views: 633

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Use square-brackets:

* myJson.field1['field.3']

Upvotes: 2

Related Questions