I am having a real hard time in attempting to iteratively append new values over a newly created JSON array/file from the same bash script. Consider the following simple script snippet:
echo "Enter name: "
read name
echo "Enter Car Model: "
read car
echo "Enter Colour: "
read colour
echo
jq -n --arg name "$name" --arg car "$car" --arg colour "$colour" '{"profiles":[{"name": $name, "car": $car, "colour": $colour}]}' > model.json
echo
cat model.json | jq .
This outputs:
Enter name:
Joe
Enter Car Model:
BMW
Enter Colour:
Red
{
"profiles": [
{
"name": "Joe",
"car": "BMW",
"colour": "Red"
}
]
}
What I want it to eventually do is something like this, dynamically:
Enter name:
Joe
Enter Car Model:
BMW
Enter Colour:
Red
{
"profiles": [
{
"name": "Joe",
"car": "BMW",
"colour": "Red"
},
{
"name": "Peter",
"car": "Nissan",
"colour": "White"
}
]
}
etc...
I understand I'll need to utilise a loop after the first set of key:value entries, but then how would I append to the newly created model.json file as opposed to overriding the initial values via jq in a bash script?
Many Thanks