使用jq根据另一个密钥名称更新json的密钥 [英] Updating key of json based on another key name using jq

查看:65
本文介绍了使用jq根据另一个密钥名称更新json的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的json文件,当且仅当制成的是 german 并且任务的值是值以 tbd-

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"]
                    }
                }
            }
        ]
        
    }
}

需要将上述json文件更新为:

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"]
                    }
                }
            }
        ]
        
    }
}

到目前为止,我试图以 german 作为键名来获取所有密钥,但是我还是不成功

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"

我可以使用类似的命令查询匹配 wheel parts

I could query the parts matching wheel, using similar command

$ jq -c 'to_entries[] | select (.value.maintenance[0].parts == "wheel") | [.key]'  json
["vehicle"]

更新:

我可以跳过检查密钥是否具有 tbd-的事情,我可以不考虑前缀而更新所有密钥名称.

I am okay to skip checking if key has tbd-, I can go update all the key names irrespective of prefix.

推荐答案

以下是使用 walk 的解决方案.如果出于某种原因,您想要一个更有针对性的解决方案(例如,一个不使用 walk 的解决方案),则应该很容易地对其进行相应的修改.

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 )

这篇关于使用jq根据另一个密钥名称更新json的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆