【问题标题】:How to access key at any level?如何访问任何级别的密钥?
【发布时间】:2021-04-09 06:02:01
【问题描述】:

我有这个输入文件

{
    "description": "this is a fake description",
    "owner": "john",
    "region": "us-east-1",
    "topics": {
        "For collecting responses for prod1": {
            "suffix_name": "response-to-hogwarts.fifo",
            "tags": {}
        }
    },
    "queues": {
        "For collecting responses and requests from test1": {
            "suffix_name": "rr_sf_name.fifo",
            "tags": {}
        },
        "For collecting responses for test2": {
            "suffix_name": "response-to-hogwarts.fifo",
            "tags": {}
        }
    },
    "subscriptions": {
        "For receiving harry_potter requests": {
            "topic": {
                "suffix_name": "harry_potter.fifo",
                "tag": "staging"
            },
            "queue": {
                "suffix_name": "rr_sf_name.fifo"
            }
        },
        "For receiving harry_potter requests from test3": {
            "topic": {
                "suffix_name": "harry_potter.fifo",
                "tag": "harry_potter_hogwarts"
            },
            "queue": {
                "suffix_name": "rr_sf_name.fifo"
            }
        },
        "For receiving demo requests": {
            "topic": {
                "suffix_name": "demo.fifo",
                "tag": "staging"
            },
            "queue": {
                "suffix_name": "rr_sf_name.fifo"
            }
        },
        "For receiving demo requests from test4 through connector": {
            "topic": {
                "suffix_name": "demo.fifo",
                "tag": "harry_potter_hogwarts"
            },
            "queue": {
                "suffix_name": "rr_sf_name.fifo"
            }
        },
        "For receiving testing responses for hogwarts": {
            "topic": {
                "suffix_name": "response-to-hogwarts.fifo"
            },
            "queue": {
                "suffix_name": "response-to-hogwarts.fifo"
            }
        }
    }
}

现在我想从 ALL suffix_name 字段的值中删除任何级别的“.fifo”后缀

示例输出(为简洁起见截断)

    "topics": {
        "For collecting responses for prod1": {
            "suffix_name": "response-to-hogwarts",
            "tags": {}
        }
    },

现在我想出了这个,对我有用

.queues |= with_entries(.value.suffix_name |= sub(".fifo";""))
| .topics |= with_entries(.value.suffix_name |= sub(".fifo";""))
| .subscriptions |= with_entries(.value |= with_entries(.value |= with_entries(.value |= rtrimstr(".fifo"))))

我想知道,是否有更好的方法,使用递归或其他方法来解析所有键,如果键是suffix_name,则从值中修剪“.fifo”。

【问题讨论】:

    标签: json nested key jq


    【解决方案1】:

    你可以使用walk:

    walk(if type=="object" and .suffix_name 
         then .suffix_name |= sub("[.]fifo$";"") else . end)
    

    或者,只需使用|=

     (.. | select(type == "object" and .suffix_name) | .suffix_name) 
      |=  sub("[.]fifo$";"") 
    

    【讨论】:

      【解决方案2】:

      使用path 函数替代walk - getpathsetpath

      reduce ( paths | select(.[-1] | endswith("suffix_name")? ) ) as $p 
        ( .; setpath($p; getpath($p) | sub("[.]fifo$";"") ) )
      

      识别从根到suffix_name 的路径并使用reduce 对其进行迭代。对于每个路径,通过删除后缀来重构值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-12
        • 2020-11-28
        • 2020-03-24
        • 2012-10-31
        • 2015-11-18
        • 2023-04-02
        • 1970-01-01
        • 2017-10-02
        相关资源
        最近更新 更多