【发布时间】:2016-06-01 04:41:35
【问题描述】:
想知道在查询子“Id”时是否可以从下面的 json 中返回父“Id”
{
"DistributionList": {
"Items": [
{
"Origins": {
"Items": [
{
"Id": "abc"
}
],
"Quantity": 1
},
"Id": "parent123"
},
{
"Origins": {
"Items": [
{
"Id": "def"
}
],
"Quantity": 1
},
"Id": "parent345"
}
]
}
}
例如。如果我查询子 ID“abc”,它应该返回“parent123”。
做类似的事情:
more jsonfile | jq '.DistributionList.Items[].Origins.Items[] | select(.Id == "abc") | .Id'
只会返回 "abc" -> 但我需要父 ID。 不知道是否有办法用 jq 做到这一点
【问题讨论】:
-
不使用jq这样的东西怎么样:
grep -oP '(?<=Id":)\s*".*$' data.txt | sed -n 's/^/child: /;N;s/\n/ has parent Id: /;p' -
@ritesht93 我只是在我的 mac 下使用该命令获取 grep 使用帮助文本