【问题标题】:Access to first key of a nested json using jq使用 jq 访问嵌套 json 的第一个键
【发布时间】:2023-01-20 01:27:42
【问题描述】:

我有以下 JSON:

{
    "query": "rest ec",
    "elected_facts_mapping": {
        "AWS": {
            "ECS": {
                "attachments": [
                    "restart_ecs"
                ],
                "text": [
                    "Great!"
                ]
            }
        }
    },
    "top_facts_mapping": {
        "AWS": {
            "ECS": {
                "attachments": [
                    "restart_ecs"
                ],
                "text": [
                    "Great!"
                ]
            },
            "EC2": {
                "attachments": [
                    "create_ec2"
                ],
                "text": [
                    "Awesome"
                ]
            }
        },
        "GitHub": {
            "Pull": {
                "attachments": [
                    "pull_req"
                ],
                "text": [
                    "Be right on it"
                ]
            }
        },
        "testtttt": {
            "test": {
                "attachments": [
                    "hello_world"
                ],
                "text": [
                    "Be right on it"
                ]
            }
        },
        "fgjgh": {
            "fnfgj": {
                "attachments": [
                    "hello_world"
                ],
                "text": [
                    "Be right on it"
                ]
            }
        },
        "tessttertre": {
            "gfdgfdgfd": {
                "attachments": [
                    "hello_world"
                ],
                "text": [
                    "Great!"
                ]
            }
        }
    },
    "elected_facts_with_prefix_text": null
}

我想访问top_facts_mapping's第一个键AWS,它是第一个键ECS

我正在尝试这样做(在我的 DSL 中):

'.span | fromjson'
'.span_data.top_facts_mapping | keys[0]'
'.span_data.top_facts_mapping[${top_facts_prepare_top_fact_topic}] | keys[0]'
'.top_facts_prepare_top_fact_topic_subtopic[${top_facts_prepare_top_fact_topic}][${top_facts_prepare_top_fact_topic_subtopic}]'

【问题讨论】:

    标签: json jq


    【解决方案1】:

    您可以使用 keys_unsorted 内置函数,因为底层对象是一个字典,并且不是一个列表

    .top_facts_mapping | keys_unsorted[0] as $k | .[$k] | .[keys_unsorted[0]]
    

    或者用一些 jq 把戏

    getpath([ paths | select(.[-3] == "top_facts_mapping" ) ] | first)
    

    由于 paths 内置将根到叶路径构造为数组,我们将所有包含倒数第二个字段(由 .[-3] 表示)的路径都作为 "top_facts_mapping" 返回路径为

    [
      [
        "top_facts_mapping",
        "AWS",
        "ECS"
      ],
      [
        "top_facts_mapping",
        "AWS",
        "EC2"
      ],
      [
        "top_facts_mapping",
        "GitHub",
        "Pull"
      ],
      [
        "top_facts_mapping",
        "testtttt",
        "test"
      ],
      [
        "top_facts_mapping",
        "fgjgh",
        "fnfgj"
      ],
      [
        "top_facts_mapping",
        "tessttertre",
        "gfdgfdgfd"
      ]
    ]
    
    

    first 从中选择列表中的第一个实体,即下面的列表

    [
      "top_facts_mapping",
      "AWS",
      "ECS"
    ]
    

    使用getpath/1获取获取路径处的JSON值

    【讨论】:

    • 不完全是我需要的。我想得到:{ "attachments": [ "restart_ecs" ], "text": [ "Great!" ] }
    • @SteveS 请更新您想要键值而不是键本身的问题
    • @SteveS 两个解决方案都已更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    相关资源
    最近更新 更多