[OpenWrt-Devel] Jsonfilter, return partial subobject?

Michael Jones mike at meshplusplus.com
Thu Apr 23 13:55:46 EDT 2020

I'm trying to work with the jsonfilter program included in the jsonpath
project from Jo-Philipp (https://git.openwrt.org/?p=project/jsonpath.gi

Perhaps this is a failure on my part to understand the documentation
provided by this referenced webpage: http://goessner.net/articles/JsonPath/

I'm trying to filter out parts of a json string while still retaining some
structured identifying information.

For example, lets say, hypothetically, I had some JSON string containing an
array of objects, and I want only two key / value pairs to be returned.

Let's use the result of "ubus call network.interface dump" as the example:

This shell script
> ubus call network.interface dump | jsonfilter -e '@.interface' |
jsonfilter -e '@[@.up=true].interface'

Will return the following, on my OpenWRT system:

What I want to do is return a JSON string with this representation:
    { "interface" : "lan", "uptime" : 11111 },

E.g., I want to filter the json not down to a single value, but to a
collection of key-value pairs by excluding items that don't match.

So, based on the documentation that I've read, the expression might look
something like this:

> ubus call network.interface dump | jsonfilter -e '@.interface' |
jsonfilter -e '@[@.up=true].[interface, uptime]'

Of course, that syntax does not work, and so far the only use of the union
operator in jsonfilter is like this expression, which returns all
interfaces (e.g. either up OR not up)

> ubus call network.interface dump | jsonfilter -e '@.interface' |
jsonfilter -e '@[@.up=true, @.up=false].interface'

I can't find any examples (beyond one or two very simplistic examples) of
how to use the jsonfilter program on stackoverflow, nor the OpenWRT mailing
list, nor the OpenWRT Discourse

I'm quite reluctant to do multiple processing sweeps. E.g. the following
(probably syntax-error-full, and erroneous output) shell script is
extremely unattractive

json=`ubus call network.interface dump | jsonfilter -e '@.interface'`
echo '['
for itf in $(echo $json | jsonfilter -e '@[@.up=true].interface')
    echo "{ \"interface\" : \"$itf\" , \"uptime\" : $(echo $json |
jsonfilter -e "@[@.interface=$itf].uptime") }," # Oh look, an unconditional
trailing comma... that won't parse until JSON5...
echo ']'

Of course, the json string that I'm trying to manipulate is not coming from
"ubus call network.interface dump", it's from a third party program that
can't be modified, I'm only using the ubus JSON as an example to illustrate
the general concept of what I'm asking for.

Is this something that jsonfilter is capable of doing at all? Or am I
having a fundamental misunderstanding of the program's capabilities?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20200423/f2304e0b/attachment.htm>
-------------- next part --------------
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org

More information about the openwrt-devel mailing list