[OpenWrt-Devel] Jsonfilter, return partial subobject?

Michael Jones mike at meshplusplus.com
Fri Apr 24 12:49:56 EDT 2020


On Thu, Apr 23, 2020 at 6:38 PM Michael Jones <mike at meshplusplus.com> wrote:

>
> On Thu, Apr 23, 2020 at 3:50 PM Jo-Philipp Wich <jo at mein.io> wrote:
>
>> Hi,
>>
>> > [...]
>> > 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.
>>
>> that is not directly possible. You can use the shell export mode together
>> with
>> the field separator to build a list of tuples safe for processing, printf
>> the
>> intermediate fields and finally use the array mode to build a proper list:
>>
>> -- 8< --
>> eval $(ubus call network.interface dump | \
>>  jsonfilter -F ': ' -e
>> 'tuples=@.interface[@.up=true]["interface","uptime"]')
>>
>> for tuple in $tuples; do
>>  printf '{ "%s": %d }\n' "${tuple%:*}" "${tuple#*:}"
>> done | jsonfilter -a -e @
>> -- >8 --
>>
>> Will result in something like the output below:
>>
>> [ { "lan": 4409874 }, { "loopback": 4409873 }, { "modem": 803939 }, {
>> "wan":
>> 4040845 }, { "wan6": 2681477 } ]
>>
>>
>> Returning subsets of objects is not directly supported unfortunately but
>> I'll
>> think about how to add something like this if I find the time.
>>
>> ~ Jo
>
>

As a follow on to this, in order to extract two values, one would need to
do something like this

eval $(ubus call network.interface dump | jsonfilter -F ':: ' -e
'tuples=@.interface[@.up=true]["interface","uptime","proto"]')

for tuple in $tuples
do
    middle=$(expr $tuple : '.*:\(.*\):.*')
    printf '{ "interface" : "%s", "uptime" : %d, "proto" : "%s"}\n'
"${tuple%%:*}" "$middle" "${tuple##*:}"
done | jsonfilter -a -e @

Notably this also breaks if the extracted text of each value contains
strings
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20200424/9f7e76c9/attachment.htm>
-------------- next part --------------
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list