Let's say I have a JSON file recipe.json
containing this:
{
"name": "Curried Lentils and Rice",
"ingredients": [
{
"quantity": "1 quart",
"name": "beef broth",
"type": "Misc"
},
{
"quantity": "1 cup",
"name": "dried green lentils",
"type": "Misc"
},
{
"quantity": "1/2 cup",
"name": "basmati rice",
"type": "Misc"
},
{
"quantity": "1 tsp",
"name": "curry powder",
"type": "Condiments"
},
{
"quantity": "1 tsp",
"name": "salt",
"type": "Condiments"
}
],
"steps": [
"Bring broth to a low boil.",
"Add curry powder and salt.",
"Cook lentils for 20 minutes.",
"Add rice and simmer for 20 minutes.",
"Enjoy!"
],
"timers": [
0,
0,
20,
20,
0
],
"imageURL": "http://dagzhsfg97k4.cloudfront.net/wp-content/uploads/2012/05/lentils3.jpg"
}
And I'm trying to insert new JSON fields into the file at specific positions in the file, using jq such as:
"cuisine": "meditaranean"
will become the 2nd entry, and
"meal": "lunch"
will become the 4th entry. So the file after the command is like this:
{
"name": "Curried Lentils and Rice",
"cuisine": "meditaranean"
"ingredients": [
{
"quantity": "1 quart",
"name": "beef broth",
"type": "Misc"
},
{
"quantity": "1 cup",
"name": "dried green lentils",
"type": "Misc"
},
{
"quantity": "1/2 cup",
"name": "basmati rice",
"type": "Misc"
},
{
"quantity": "1 tsp",
"name": "curry powder",
"type": "Condiments"
},
{
"quantity": "1 tsp",
"name": "salt",
"type": "Condiments"
}
],
"meal": "lunch"
"steps": [
"Bring broth to a low boil.",
"Add curry powder and salt.",
"Cook lentils for 20 minutes.",
"Add rice and simmer for 20 minutes.",
"Enjoy!"
],
"timers": [
0,
0,
20,
20,
0
],
"imageURL": "http://dagzhsfg97k4.cloudfront.net/wp-content/uploads/2012/05/lentils3.jpg"
}
My question is how to do this with jq?
Note: This other question addresses performing updates of single fields, while the current question is about inserts. They are as different as orange and yellow bell peppers, which is different! (Don't make me add a picture of bell peppers to this post, I swear I'll do it if I have to.)