Filtering values from Json format without jq

Hi
Im checking inside a openwrt to see If is possible to filter json string to have the values.

For example {"address":"192.168.2.2","user":"user1","groups":"permissions"}

I receive from curl the string and I need to separate values to pass vars to other commands.

For now Im checking some examples but not works

#!/bin/sh
. /usr/share/libubox/jshn.sh 
json_init
json_load '$(cat $STRING)'
json_get_keys keys
for k in $keys; do 
   json_get_var v "$k"
   echo "$k : $v"
done

But produce error "Failed to parse message data"

My problem is justly that I can't use jq, or python to choose data, so I think the solution is to separate first and send to vars every value, making something usable. I was checking jshn and jsonfilter utilities but for this type of json not seems to work.

Suggestions?

Google something like "bash split json" and you will find tons of examples on how to do that.

Hi

For now I detected first error

json_load '$(cat $RESPONSE)' needs to be

json_load $(echo $RESPONSE)

You can use OpenWrt’s builtin jsonfilter command.

eval $(jsonfilter -s '{"address":"192.168.2.2","user":"user1","groups":"permissions"}' -e 'ADDRESS=@.address' -e 'USER=@.user')
echo "address=$ADDRESS user=$USER"
1 Like

Great!!

I prefer this option to filter, but I never found the form to use -e option.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.