Return 查询其子节点之一时带有 jq 的父节点

Return parent node with jq while querying one of its children

想知道在查询子 "Id"

时是否可以从下面的 json return 父 "Id"
{
    "DistributionList": {
        "Items": [
            {
                "Origins": {
                    "Items": [
                        {
                            "Id": "abc"
                        }
                    ],
                    "Quantity": 1
                },
                "Id": "parent123"
            },
            {
                "Origins": {
                    "Items": [
                        {
                            "Id": "def"
                        }
                    ],
                    "Quantity": 1
                },
                "Id": "parent345"
            }
         ]
    }
}

例如。如果我查询子 ID "abc" 它应该 return "parent123".

做类似的事情:

more jsonfile | jq '.DistributionList.Items[].Origins.Items[] | select(.Id == "abc") | .Id'

只会 return "abc" -> 但我需要父 ID。 不确定是否有办法用 jq

做到这一点

过滤器: .. |对象 | select(.Origins.Items[]? | .Id == "abc") | .Id

产生:

"parent123"

您可能想要参数化过滤器,例如:

def parent(child):
 .. | objects | select( .Origins.Items[]? | .Id == child) | .Id ;

原题中的过滤器接近解。所需要做的就是重新排列 select 中的内容。例如

  .DistributionList.Items[]
| select(.Origins.Items[].Id == "abc")
| .Id