Issue
I have this JSON file :
{
"range": "Sheet!A2:B100",
"majorDimension": "ROWS",
"values": [
[
"customer1_name",
"[email protected]",
"customer1_phone",
"customer1_city"
],
[
"customer2_name",
"[email protected]",
"customer2_phone",
"customer2_city"
]
]
}
And, from a shell script, I would like to use JQ to extract every value for every block and assign them to some variables. here is expected result :
For this block :
[
"customer1_name",
"[email protected]",
"customer1_phone",
"customer1_city"
],
Result should be like this :
VAR1 = "customer1_name"
VAR2 = "[email protected]"
VAR3 = "customer1_phone"
VAR4 = "customer1_city"
And for this block :
[
"customer2_name",
"[email protected]",
"customer2_phone",
"customer2_city"
],
Result should be like this :
VAR1 = "customer2_name"
VAR2 = "[email protected]"
VAR3 = "customer2_phone"
VAR4 = "customer2_city"
The idea is to read JSON file block by block to get/retrieve all the values inside VARS.
Solution
Using tr
like this is a bit hacky, but:
$ cat input
{
"range": "Sheet!A2:B100",
"majorDimension": "ROWS",
"values": [
[
"customer1_name",
"[email protected]",
"customer1_phone",
"customer1_city"
],
[
"customer2_name",
"[email protected]",
"customer2_phone",
"customer2_city"
]
]
}
$ jq -rc '.values[]' input | tr -d '["]' | while IFS=, read var1 var2 var3 var4; do echo "$var1 $var2 $var3 $var4"; done
customer1_name [email protected] customer1_phone customer1_city
customer2_name [email protected] customer2_phone customer2_city
You can also do:
$ jq -rc '.values[][]' input | while read name; read email; read phone; read city; do echo "$name $email $phone $city"; done;
customer1_name [email protected] customer1_phone customer1_city
customer2_name [email protected] customer2_phone customer2_city
Answered By - William Pursell Answer Checked By - Marie Seifert (WPSolving Admin)