JSONPath Syntax when dot in key

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.


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.


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.

Tags:

Json

Jsonpath