Would appreciate some advice, I am querying a IOT devices api which returns a JSON file however, order of the data is not always in sequentially output.
I have successfully extracted the value number from the json using bash and concatenated and store this into a variable. In order to sort and store the data I want to be able to create a variable based on the value of the concatenated variable. I'm using jq to query the json and array index to query the different objects in JSON and created $id:
did=myvar$id
echo $did
myvar10
This is where i'm stuck now, i want to be able to create a new variable from the value stored in $did
As an example,
- if i echo $did and this returns myvar10, i require a new variable created to be called $myvar10.
- If i echo $dis and this returns myvar8, i require a new variable created to be called $myvar8.
please can someone help push me in the right direction?
TIA
declare -A myvararray ; id=10 ; did="myvar$id" ; myvararray[$did]="something" ; echo "${myvararray["myvar10"]}"
(outputs "something"). – frabjous Oct 21 '22 at 00:12jq
inbash
, creating variables with odd names taken from user-supplied data is not the way to go.jq
can sort, extract, and do all sorts of operations on JSON documents. – Kusalananda Oct 21 '22 at 09:41