jq recursively update values for certain elements

别等时光非礼了梦想. 提交于 2021-02-10 17:27:37

问题


The intent for the JSON data below is to update the value of the field dst with the value of src within all elements of type t, regardless of depth within the tree, while at the same time preserving the whole structure of the data.

Is this possible with jq? My several attempts have boiled down to the following command that is not working to achieve the intended purpose:

$ jq -r 'map_values(select(.. | .type? == "t" |= (.dst = .src)))'

{
    "a": "b",
    "c": [
        {
            "type": "t",
            "src": "xx",
            "dst": "zz"
        },
        {
            "type": "t",
            "src": "xx",
            "dst": "zz"
        }
    ],
    "d": [
        {
            "e": [
                {
                    "type": "t",
                    "src": "xx",
                    "dst": "zz"
                }
            ]
        },
        {
            "type": "t2",
            "src": "xx",
            "dst": "zz"
        }
     ]
}

回答1:


Is this possible with jq?

jq is Turing-complete :-)

Here's a simple solution:

walk( if type == "object" and .type == "t" then .dst = .src else . end)

If your jq does not have walk/1, then it might be a good time to upgrade (to jq 1.6); otherwise, you can snarf its def from the web, e.g. by googling: jq "def walk"

Alternatively ...

reduce paths as $x (.;
    if (getpath($x)|.type? // false) == "t"
    then setpath( $x + ["dst"]; getpath( $x + ["src"] ))
    else . end)


来源:https://stackoverflow.com/questions/53145680/jq-recursively-update-values-for-certain-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!