Forum Discussion

Asubbiah's avatar
Asubbiah
Contributor
9 years ago

parse the json response with field name containing slash '/'

Hi, I have to parse the json response and get the value of a field "bookName": "MyBook 001" Here is my response:   { "meta": { "/data/library": { "bookName": "MyBook 001", "/book/ID": { "rea...
  • jmueller's avatar
    9 years ago

    This is primarily a groovy question. There are actually a couple of ways you can handle this.

    The simplest change is just to quote your fields, so change your line below to this:

    def bName =jsonResponse.meta."data/library".bookName

    It looks weird, but you need quotes because / is a valid character for identifiers in groovy, but not in java.

    Here is the documentation for this feature.

    http://groovy-lang.org/syntax.html#_quoted_identifiers

     

    The documentation for the JsonSlurper class says that the parseText method returns maps and lists, so you could optionally use the native syntax for those classes like jsonResponse["/data/library"]["bookName"] but then you are using quotes anyway.

     

    Jack