Search code examples
jsonjsonata

Matching a json field with a name that contains spaces or special charecters with Jsonata


Lets say I have some JSON like:

{
  "normal": 1,
  "has some spaces": 2,
  "@": 3
}

I can get the value of the normal field with the simple query:

normal

However I can't get the other two fields. I have tried:

[has some spaces]
'has some spaces'
['has some spaces']
"has some spaces"
["has some spaces"]
{has some spaces}
{'has some spaces'}
{"has some spaces"}

but none of them work


Solution

  • This was resolved for me by the maintainer on a github issue I raised :

    Under the Navigating JSON Objects section, the following is listed:

    Field references containing whitespace or reserved tokens can be enclosed in backticks

    ...

    then the JSONata expressions `has some spaces` and `@` will match 2 and 3 respectively.

    But not before I discovered two alternatives by trial and error:

    $."has some spaces"
    

    and

    $.'has some spaces'