I am pretty new to jq command and stuck at a place to edit a JSON file. I have a JSON file in the format below.
{
"service": {
"name": "web",
"tags": [
"contact_points"
],
"check": {
"script": "tmp/status_check.py > /dev/null 2>&1",
"interval": "10s"
}
}
}
I want to modify this JSON to add a nested key/value as following:
{
"service": [{
"name": "web",
"tags": [
"contact_points"
],
"check": {
"script": "tmp/status_check.py > /dev/null 2>&1",
"interval": "10s"
}
},
{
"name": "tomcat",
"tags": [
"contact_points"
],
"check": {
"script": "tmp/status_check.py > /dev/null 2>&1",
"interval": "10s"
}
}
]
}
I tried the below command but it overwrites the contents of the files.
jq '. + { "service": "{"name":"tomcat","tags":["contact_points"],"check":{"script":"tmp/status_check.py > /dev/null 2>&1","interval":"10s"}}" }' /tmp/status.json > /tmp/file && mv /tmp/file /tmp/status.json
and gives the below output
{
"service": {
"name": "tomcat",
"tags": [
"contact_points"
],
"check": {
"script": "tmp/status_check.py > /dev/null 2>&1",
"interval": "10s"
}
}
}
I tried escaping the special characters but was not able to get the desired output. Is there any other way of achieving this? any help is greatly appreciated.
{"service":...}objects in an array, give the second service a different key or perhaps emit two separate objects in a stream.