I'm trying to write a script for comissioning embedded devices, they retrieve a JSON object from an API that contains an array of scripts that must be run to comission the device.
{
"status":"wait",
"settings":{
"serialNo": "123456",
"macAddress":"ff:ff:ff:ff:ff:ff",
"ipMode": "static|dhcp",
"ipAddress": "192.168.0.1",
"ipSubnet": "255.255.255.0",
"ipGateway": "192.168.0.10",
"ipDns": "192.168.0.10"
},
"scripts":[
"https://www.google.co.uk/1",
"https://www.google.co.uk/2",
"https://www.google.co.uk/3"
]
}
As the devices run minimal linux installs with busybox I am using sed to "parse" the JSON and retrieve the values from the object. This works fine for single parameters such as
mac=$(echo $reply | sed -ne 's/^.*"macAddress":"\([^"]*\)".*$/\1/p')
echo $mac
ff:ff:ff:ff:ff:ff
I try to use a similar regex to match the contents of the array between [
and ]
but when I run it through sed it returns with nothing.
scripts=$(echo $reply | sed -ne 's/"scripts":\(\[[^\[\]]*\]\)/\1/p')
echo $scripts
What I would like it to result in is this:
echo $scripts
["https://www.google.co.uk/1","https://www.google.co.uk/2","https://www.google.co.uk/3"]
With jq
you can issue the following command:
jq -r '.scripts[]' the.json
If you want to put this into an array, use command substitution:
arr=( $(jq -r '.scripts[]' a.json ) )
Now you can access the individual urls using:
echo "${arr[0]}"
echo "${arr[1]}"
echo "${arr[2]}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.