[OpenWrt-Devel] Jsonfilter, return partial subobject?

Michael Jones mike at meshplusplus.com
Thu Apr 23 19:38:45 EDT 2020


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
>
>
Thank you very much for the suggestion on using the -F flag, and the printf
command.

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.

Please let me know if I can help with adding more capabilities to the
jsonfilter program.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20200423/dfc56210/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