On Thu, Apr 23, 2020 at 6:38 PM Michael Jones <[email protected]> wrote:
> > On Thu, Apr 23, 2020 at 3:50 PM Jo-Philipp Wich <[email protected]> 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 >> '[email protected][@.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 '[email protected][@.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
_______________________________________________ openwrt-devel mailing list [email protected] https://lists.openwrt.org/mailman/listinfo/openwrt-devel
