<div dir="ltr"><div dir="ltr"></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Apr 23, 2020 at 3:50 PM Jo-Philipp Wich <<a href="mailto:jo@mein.io">jo@mein.io</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
> [...]<br>
> What I want to do is return a JSON string with this representation:<br>
> [<br>
> { "interface" : "lan", "uptime" : 11111 },<br>
> ...<br>
> ]<br>
> <br>
> E.g., I want to filter the json not down to a single value, but to a<br>
> collection of key-value pairs by excluding items that don't match.<br>
<br>
that is not directly possible. You can use the shell export mode together with<br>
the field separator to build a list of tuples safe for processing, printf the<br>
intermediate fields and finally use the array mode to build a proper list:<br>
<br>
-- 8< --<br>
eval $(ubus call network.interface dump | \<br>
jsonfilter -F ': ' -e 'tuples=@.interface[@.up=true]["interface","uptime"]')<br>
<br>
for tuple in $tuples; do<br>
printf '{ "%s": %d }\n' "${tuple%:*}" "${tuple#*:}"<br>
done | jsonfilter -a -e @<br>
-- >8 --<br>
<br>
Will result in something like the output below:<br>
<br>
[ { "lan": 4409874 }, { "loopback": 4409873 }, { "modem": 803939 }, { "wan":<br>
4040845 }, { "wan6": 2681477 } ]<br>
<br>
<br>
Returning subsets of objects is not directly supported unfortunately but I'll<br>
think about how to add something like this if I find the time.<br>
<br>
~ Jo<br><br></blockquote><div><br></div><div>Thank you very much for the suggestion on using the -F flag, and the printf command.<br><br>This is less verbose than the jshn.sh script that I wrote in my followup email, so it will probably be what I use for now.<br><br>Please let me know if I can help with adding more capabilities to the jsonfilter program. </div></div></div>