Given this document:
{
"slide": [{
"title": {
"fontName": "Open Sans",
"top": 188,
"left": -22,
"width": 597,
"fontSize": 45,
"valign": "bottom",
"presetType": "Parallelogram",
"fill": "#6bcad5",
"halign": "left",
"fontColor": "#f4dde6",
"height": 192
}
}, {
"picture": {
"top": 25,
"left": 54,
"width": 1,
"colorMode": "GRAYSCALE",
"presetType": "Snip_Same_Side_Rect",
"height": 1
}
}]
}
The following code returns [54]
:
JSONArray obj = ctx.read("$.slide[?(@.picture)].picture.left");
But I need a primitive type, still maintaining the indefinite path.
According to the docs:
Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.
Note: if you are using the Java implementation then an issue has already been raised for this and the response to that issue reiterated the above point.
So, as long as you use a filter you'll need two calls, for example:
String json = "...";
DocumentContext ctx = JsonPath.parse(json);
// capture the JSONArray
JSONArray obj = ctx.read("$.slide[?(@.picture)].picture.left");
// read the first value from the JSONArray
// prints "54"
System.out.println(obj.get(0));
// alternatively, push the JSON representation of the JSONArray back through JsonPath
Integer value = JsonPath.read(obj.toJSONString(), "$[0]");
// prints 54
System.out.println(value);