Issue
I am trying to parse values from JSON file that takes the following form (config.json):
[{
"gain": "90",
"exposure": "850",
"whitebalanceR": "120",
"whitebalanceG": "185",
"whitebalanceB": "100",
"Snap": "True"
}
]
I parse each individual string like this (app.sh):
grep -o '"gain": "[^"]*' config.json | grep -o '[^"]*$'
grep -o '"exposure": "[^"]*' config.json | grep -o '[^"]*$'
grep -o '"whitebalanceR": "[^"]*' config.json | grep -o '[^"]*$'
grep -o '"whitebalanceG": "[^"]*' config.json | grep -o '[^"]*$'
grep -o '"whitebalanceB": "[^"]*' config.json | grep -o '[^"]*$'
This will output the value of the string in the terminal I would like to use the value for each string as input to some function like this:
./someFunc -w '${whitebalanceR} ${whitebalanceG} ${whitebalanceG}' -ax6 -s ${exposure} -g ${gain} -i 1 -o -a -6
But it fails to run? What am I doing wrong? and how to actually use the JSON string as input to a function in bash? Much appreciated
Solution
IMHO, use bash is a bad idea for parsing JSON. But, if you have no choice, use jq
as Tranbi suggested:
gain=$( jq -r '.[] | .gain' config.json; )
echo $gain
-r
option is required to print the value without quotes.
Hope that helps!
Answered By - Emil Viesná Answer Checked By - Gilberto Lyons (WPSolving Admin)