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

柔情痞子 提交于 2021-02-17 02:05:01

问题


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

回答1:


Use square-brackets:

* myJson.field1['field.3']


来源:https://stackoverflow.com/questions/62404067/access-a-json-field-value-if-the-json-field-has-special-chars-as-dots

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!