Issue
Wondering if its possible to return the parent "Id" from the json below while querying the child "Id"
{
"DistributionList": {
"Items": [
{
"Origins": {
"Items": [
{
"Id": "abc"
}
],
"Quantity": 1
},
"Id": "parent123"
},
{
"Origins": {
"Items": [
{
"Id": "def"
}
],
"Quantity": 1
},
"Id": "parent345"
}
]
}
}
Eg. If I query for child id "abc" it should return "parent123".
Doing something like:
more jsonfile | jq '.DistributionList.Items[].Origins.Items[] | select(.Id == "abc") | .Id'
will only return "abc" -> but i need the parent id. Not sure if there is way to do this with jq
Solution
The filter:
.. | objects | select(.Origins.Items[]? | .Id == "abc") | .Id
produces:
"parent123"
You might want to parameterize the filter, e.g.:
def parent(child):
.. | objects | select( .Origins.Items[]? | .Id == child) | .Id ;
Answered By - peak Answer Checked By - Gilberto Lyons (WPSolving Admin)