20

Please forgive me if I use the incorrect terminology, I am quite the novice.

I have some simple JSON:

{
"properties": {
    "footer.navigationLinks": {
        "group": "layout"
    ,   "default": [
            {
                "text": "Link a"
            ,   "href": "#"
            }
        ]
    }
}
}

I am trying to pinpoint "footer.navigationLinks" but I am having trouble with the dot in the key name. I am using http://jsonpath.com/ and when I enter

$.properties['footer.navigationLinks']

I get 'No match'. If I change the key to "footernavigationLinks" it works but I cannot control the key names in the JSON file.

Please can someone help me target that key name?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Hayley
  • 221
  • 1
  • 2
  • 7
  • have you tried `$["properties"]["footer.navigationLinks"]` ? -- http://www.baeldung.com/guide-to-jayway-jsonpath – Rob Scott Oct 22 '16 at 01:06
  • JSONPath has been forked and made available several languages (JavaScript, Node, PHP, Python, Java). Which JSONPath implementation are you using? – trincot Oct 22 '16 at 08:19
  • @Rob Yes, I have tried `$["properties"]["footer.navigationLinks"]` but that also returns 'No Match'. @trincot I believe its Javascript. I'm assuming if I can get it to work in jsonpath.com then it will work in my project. Thanks for your replies – Hayley Oct 22 '16 at 09:33

4 Answers4

11

For information, jsonpath.com has been patched since the question was asked, and it now works for the example given in the question. I tried these paths successfully:

  • $.properties['footer.navigationLinks']
  • $.properties.[footer.navigationLinks]
  • $.properties.['footer.navigationLinks']
  • $['properties']['footer.navigationLinks']
  • $.['properties'].['footer.navigationLinks']
  • properties.['footer.navigationLinks']
  • etc.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
10

Having a json response:

{
  "0": {
    "SKU": "somevalue",
    "Merchant.Id": 234
    }
}

I can target a key with a . (dot) in the name.

jsonPath.getJsonObject("0.\"Merchant.Id\"")

Note: the quotes and the fact that they are escaped.

Note not sure of other versions, but I'm using

'com.jayway.restassured', name: 'json-path', version: '2.9.0'

A few samples/solutions I've seen, was using singe quotes with brackets, but did not work for me.

Kotie Smit
  • 646
  • 1
  • 5
  • 18
4

This issue was reported in 2007 as issue #4 - Member names containing dot fail and fixed.

The fix is not present in this online jsonpath.com implementation, but it is fixed in this old archive and probably in most of the forks that have been created since (like here and here).

Details about the bug

A comparison between the buggy and 2007-corrected version of the code, reveals that the correction was made in the private normalize function.

In the 2007-corrected version it reads:

normalize: function(expr) {
    var subx = [];
    return expr.replace(/[\['](\??\(.*?\))[\]']|\['(.*?)'\]/g, function($0,$1,$2){
        return "[#"+(subx.push($1||$2)-1)+"]";
    })  /* http://code.google.com/p/jsonpath/issues/detail?id=4 */
    .replace(/'?\.'?|\['?/g, ";")
    .replace(/;;;|;;/g, ";..;")
    .replace(/;$|'?\]|'$/g, "")
    .replace(/#([0-9]+)/g, function($0,$1){
         return subx[$1];
    });
},

The first and last replace in that sequence make sure the second replace does not interpret a point in a property name as a property separator.

I had a look at the more up-to-date forks that have been made since then, and the code has evolved enormously since.

Conclusion:

jsonpath.com is based on an outdated version of JSONPath and is not reliable for previewing what current libraries would provide you with.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thanks so much for the explanation, it seems that I was on the right track after all so that's encouraging. – Hayley Oct 22 '16 at 20:11
0

You can encapsulate the 'key with dots' with single quotes as below

response.jsonpath().get("properties.'footer.navigationLinks'")

Or even escape the single quotes as shown:

response.jsonpath().get("properties.\'footer.navigationLinks\'")

Both work fine