Introduction
jq
is a command-line JSON processor that you can use to filter, transform, read or create JSON data. In this short article, I want to show how can you create JSON data using jq command line when working with a bash script. Traditionally, if you want to create simple JSON data without using jq, you can do it this way using bash (not recommended as it is not perfect):
#!/bin/bash
# This code is not perfect!
# Initialize a valid json array called "data" and save it in a file called "data.json"
echo '{"data":[' > data.json
# Loop through the data and append it to the json array
for i in $(seq 1 10); do
echo '{"id":'$i',"name":"name'$i'","value":'$i'},' >> data.json
done
# Close the json array
echo ']}' >> data.json
# Print the json array
cat data.json
The above will output this:
root@MAXIWORK:~# ./x
{"data":[
{"id":1,"name":"name1","value":1},
{"id":2,"name":"name2","value":2},
{"id":3,"name":"name3","value":3},
{"id":4,"name":"name4","value":4},
{"id":5,"name":"name5","value":5},
{"id":6,"name":"name6","value":6},
{"id":7,"name":"name7","value":7},
{"id":8,"name":"name8","value":8},
{"id":9,"name":"name9","value":9},
{"id":10,"name":"name10","value":10},
]}
As you notice from the above example, you can trim the last element using another tool such as sed because it should not have a comma at the end as that would make the JSON syntax to be invalid. Let us not make this complicated. We have a solution to overcome this by using the powerful jq command line! See the example below:
#!/bin/bash
# Create a valid json array called "data" and save it in a file called "data.json"
output_file='data.json'
# Create an initial block
jq -n '{"data":[]}' > "${output_file}"
# Loop through the data and append it to the json array
for i in $(seq 1 10); do
#echo '{"id":'$i',"name":"name'$i'","value":'$i'},' >> data.json #Previous code
json_data=$(jq --arg name "name${i}" --argjson value "$i" '.data += [{ "name": $name, "value": $value }]' "${output_file}")
echo "${json_data}" > "${output_file}"
done
The important part of the above code is the update assignment keyword +=
You can use this update assignment to append existing JSON data. In this case, we were referring to .data on the left-hand side and adding the new value from the right-hand side
The –arg command is used when the variable contains string whereas –argjson is used when the value is integer
The output from the above code is indented and valid
{
"data": [
{
"name": "name1",
"value": 1
},
{
"name": "name2",
"value": 2
},
{
"name": "name3",
"value": 3
},
{
"name": "name4",
"value": 4
},
{
"name": "name5",
"value": 5
},
{
"name": "name6",
"value": 6
},
{
"name": "name7",
"value": 7
},
{
"name": "name8",
"value": 8
},
{
"name": "name9",
"value": 9
},
{
"name": "name10",
"value": 10
}
]
}