Have bash variable contain every execution one level only but not know JSON structure, example:
$ echo $JJJ
{ "media": "heat", "meter": "kamheat", "name": "Heato", "id": "32323232", "flow_return_temperature_difference_c": 37.06, "forward_energy_m3c": 67136, "max_flow_m3h": 22.75, "max_power_kw": 869, "on_time_h": 17232, "on_time_at_error_h": 28, "power_kw": 560, "return_energy_m3c": 37929, "t1_temperature_c": 81.37, "t2_temperature_c": 44.31, "target_energy_kwh": 3200000, "target_volume_m3": 81847, "total_energy_consumption_kwh": 3363200, "total_volume_m3": 85656, "volume_flow_m3h": 13.16, "fabrication_no": "84853230", "meter_datetime": "2024-01-15 11:46", "status": "OK", "target_date": "2024-01-01", "timestamp": "2025-01-16T14:05:13Z" }
Next time can have other one level JSON with unknown pairs like eg:
{ "media": "water", "meter": "aptnaa1", "name": "Meter", "id": "56434345", "max_flow_m3h": 1.7,"on_time_h": 17232, "on_time_at_error_h": 28, "target_volume_m3": 1200, "total_volume_m3": 965.78, "volume_flow_m3h": 3.16, "fabrication_no": "89853230", "meter_datetime": "2024-01-12 11:46", "status": "OK", "target_date": "2024-01-01", "timestamp": "2025-01-16T14:05:13Z" }
I’d like to convert the JSON into bash array
declare -A array_telegram=()
and for each JSON pair put the pairs into array like this:
array_telegram+=(["media"]="heat")
array_telegram+=(["meter"]="kamheat")
.....
array_telegram+=(["timestamp"]="2025-01-16T14:05:13Z")
How to put not-know JSON structure into bash array?
Try use jq but without success.
3
Answers
Use a JSON processor like jq, and have it e.g. produce the
declare
statement:With newer versions of Bash you can also use the
key value
form for declaration:Both produce the populated and ready-to-use associative bash array:
A more verbose form of @pmf’s answer: a while loop to read the values from the jq output
To parse unknown JSON structure into a Bash associative array (declare -A) dynamically, you can use jq to extract the key-value pairs and populate the array. Here’s how you can achieve it:
So to explain to you :
The jq -r ‘to_entries | .[] | "(.key)=(.value)"’ command converts JSON into key-value pairs in the format key=value.
A while loop reads each key-value pair, using IFS="=" to split the line.
sed removes surrounding quotes from values if they exist.
each key-value pair is added to the array_telegram array.
Output
For the given JSON, the output will look like this: