[
{ "id":"1",
"metadata":{"version":"1.0","author":"user1"}
},
{ "id":"2",
"metadata":{"version":"1.0","author":"user2","timestamp":"2019-02-05"}
},
{ "id":"3",
"metadata":{"version":"1.0","author":"user3","price":"10.0"}
}]
I am trying to parse the above json (data1.json) using jq, but I am unable to access the metadata object. I will eventually use the metadata object to insert into a database as a string. It can have any number of fields, the structure is not fixed. This is the script that I'm using.
#!/usr/bin/env bash
id=($(jq '.[] | .id' data1.json | tr -d '"'))
metadata=($(jq '.[] | .metadata' data1.json))
n_id=${#id[@]}
n_meta=${#metadata[@]}
echo $n_id
echo $n_meta
for (( i=0; i<n_id; i++ )); do
echo ${metadata[$i]}
done
Expected output:
{"version":"1.0","author":"user1"}
{"version":"1.0","author":"user2","timestamp":"2019-02-05"}
{"version":"1.0","author":"user3","price":"10.0"}
What am I doing wrong here? Any help is appreciated.