简体   繁体   中英

Jayway JSONPath: how to select terminal nodes

I'm using Jayway JSONPath.

Given a JSON document having nodes with the same name at different structure levels, how would I select only those nodes that are terminal nodes, ie having only text or no content?

XPath would allow not(child::*) as a predicate, but I can't see a JSONPath equivalent.

Unfortunately, no JSONPath implementation (as of now) offers such an operation. However, some of the more advanced implementations that expanded on Goessner's reference have operations that get close to this.

One workaround is to use check the type of a node, if possible. For instance this is possible in the JavaScript JSONPath-Plus implementation using Type selectors for JSON types: eg @null() , @boolean() , @number() , @string() , @array() , @object() @integer() and others. This allow us for instance to get only numeric values:

$..*@number()

Combined with a more meaningful path selection we might get close. Nonetheless, this will not yield terminal values only but at least avoids array and object type properties.

Another workaround that is should work with basic data types is to use the regex matcher available in quite a few implementations (like JayWays, many JavaScript implementations, etc) to interpret the type of a node, eg again let's say numeric values

$..[?(@.price =~ /[0-9]+\.?[0-9]*/)]

Again, this will not give you terminal values only but avoids array and object type properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM