I have the below json in a file from which I wanted to take the "ids" from all price object and put into an array variable.
{
"documentType": "Prices",
"fullCharges": [
{
"ResourceId": null,
"price": {
"href": null,
"id": "8ddaaabc92bc"
},
"product": {
"href": null,
"id": "123"
}
},
{
"price": {
"href": null,
"id": "326f0f273258"
},
"product": {
"href": null,
"id": "123"
}
}
],
"createdBy": "test",
"createdOn": "2021-10-05T00:00:55Z",
"currentSeqNum": 2
}
I am using the below query but the result is coming in the proper way.
priceIds=$(jq -r .fullCharges[].price.id ${file})
Using above command, it is behaving like a single value, not like array. If I print the priceId value it is only showing the last value.
326f0f273258 instead of 8ddaaabc92bc 326f0f273258
And when I am looping over it, again it is behaving as a single value.
for price in "${priceIds[@]}"
do
printf "$price"
cbq -u Administrator -p Administrator -e "http://localhost:8093" --script="select * FROM \`com.src.test.price\` where docId==\"$price\";"
done
Output command of above loop: select * FROM `com.src.test.price` where documentId=="8ddaaabc92bc 326f0f273258";
There should be 2 command like these
select * FROM `com.src.test.price` where documentId=="8ddaaabc92bc"
select * FROM `com.src.test.price` where documentId=="326f0f273258"
CodePudding user response:
- Using extra parens, it helped me to create array.
- Using the below sed command over array item variable, i was able to remove extra line from it.
price=$(echo "$price" | sed 's/^[ \n]*//;s/[ \n]*$//')