rodee
rodee

Reputation: 3181

Updating key of json based on another key name using jq

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.

Upvotes: 0

Views: 707

Answers (2)

Fábio Almeida
Fábio Almeida

Reputation: 305

If you don't have jq 1.6 , which has walk/1 , do prepend the def walk :

jq '
# Apply f to composite entities recursively, and to atoms
    def walk(f):
    . as $in
    | if type == "object" then
        reduce keys[] as $key
            ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
    elif type == "array" then map( walk(f) ) | f
    else f
    end;
  walk( if type=="object" and .task and (.task|startswith("tbd-")) and any(.[]; type=="object" and has("german")) then .task|=sub("tbd-"; "premium-") else . end )
' filename

source: https://github.com/stedolan/jq/issues/963#issuecomment-152783116

Upvotes: 1

peak
peak

Reputation: 116977

Here's a solution using walk. If for some reason you want a more targeted solution (for example, one that does not use walk), it should be easy to modify it accordingly.

walk( if type=="object" and .task and (.task|startswith("tbd-")) and
         any(.[]; type=="object" and has("german"))
      then .task|=sub("tbd-"; "premium-")
      else . end )

Upvotes: 2

Related Questions