I have below json file, need to replace tbd-
with premium-
in task's value if and only if made is german
and task's value starts with tbd-
{
"vehicle": {
"maintenance": [
{
"parts": "wheel",
"size": ["one", "two"]
},
{
"task": "tbd-service-oil",
"car": {
"german": {
"audi": ["Synthetic"]
}
},
"some": ["other"]
},
{
"task": "service-oil",
"honda": {
"japan": {
"oil": ["regular"]
}
}
}
],
"repair": [
{
"parts": "wheel",
"size": ["one", "two"]
},
{
"task": "tbd-engine-repair",
"car": {
"german": {
"engine": ["6-cyl"]
}
}
},
{
"task": "engine-repair",
"car": {
"german": {
"engine": ["4-cyl"]
}
}
}
]
}
}
need to update above json file to:
{
"vehicle": {
"maintenance": [
{
"parts": "wheel",
"size": ["one", "two"]
},
{
"task": "premium-service-oil", ## update this b'cos there is "german" under "car" and task's value had prefix "tbd-"
"car": {
"german": {
"audi": ["Synthetic"]
}
},
"some": ["other"]
},
{
"task": "service-oil",
"honda": {
"japan": {
"oil": ["regular"]
}
}
}
],
"repair": [
{
"parts": "wheel",
"size": ["one", "two"]
},
{
"task": "premium-engine-repair", ## update this b'cos there is "german" under "car" and task's value had prefix "tbd-"
"car": {
"german": {
"engine": ["6-cyl"]
}
}
},
{
"task": "engine-repair", ### no need to update this as it don't have "tbd-" as prefix
"car": {
"german": {
"engine": ["4-cyl"]
}
}
}
]
}
}
so far, I tried to get all the keys with german
as keyname, I am not successful though
jq -c 'to_entries[] | select (.key.maintenance.car == "german") | [.key]' json
jq: error (at json:50): Cannot index string with string "maintenance"
I could query the parts
matching wheel
, using similar command
$ jq -c 'to_entries[] | select (.value.maintenance[0].parts == "wheel") | [.key]' json
["vehicle"]
UPDATE:
I am okay to skip checking if key has tbd-
, I can go update all the key names irrespective of prefix.