I have a system that outputs multiple types of JSON files with data.
Is there any chance to implement the three conditions below using jq
? I am exploring the map()
command, and the --arg
and --argjson
command line options, but I am having a tough time.
First JSON, where I have an asset
object and I want to add following key-value pair:
"house": true and "colour": "orange"
{
"name": "Paul",
"country": "USA",
"spec": {
...
"asset": {
"yard": true
},
}
}
Desired output:
{
"name": "Paul",
"country": "USA",
"spec": {
...
"asset": {
"house": true,
"colour": "orange",
"yard": true
},
}
}
2nd JSON, where I don't have an asset
object, so I want to add the object and the following key-value pair
"house": true and "colour": "orange"
{
"name": "Paul",
"country": "USA",
"spec": {
...
}
}
Desired output:
{
"name": "Paul",
"country": "USA",
"spec": {
...
"asset": {
"house": true,
"colour": "orange"
},
}
}
3rd JSON, where I have an asset
object, but I would like to change the key-value pair of house
if it is set to false
and set colour
to orange
:
{
"name": "Paul",
"country": "USA",
"spec": {
...
"asset": {
"house": false,
"colour": "black"
},
}
}
Desired output:
{
"name": "Paul",
"country": "USA",
"spec": {
...
"asset": {
"house": true,
"colour": "orange"
},
}
}