<div dir="ltr"><div dir="ltr"></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Apr 23, 2020 at 6:38 PM Michael Jones <<a href="mailto:mike@meshplusplus.com">mike@meshplusplus.com</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"><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" target="_blank">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</blockquote></div></div></blockquote><div><br></div><div><br></div><div>As a follow on to this, in order to extract two values, one would need to do something like this</div><div><br></div>eval $(ubus call network.interface dump | jsonfilter -F ':: ' -e 'tuples=@.interface[@.up=true]["interface","uptime","proto"]')<br><br>for tuple in $tuples<br>do<br>    middle=$(expr $tuple : '.*:\(.*\):.*')<br>    printf '{ "interface" : "%s", "uptime" : %d, "proto" : "%s"}\n' "${tuple%%:*}" "$middle" "${tuple##*:}"<br><div>done | jsonfilter -a -e @ </div><div><br></div><div>Notably this also breaks if the extracted text of each value contains strings</div></div></div>