Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 524

Consolidating JOIN match results as opposed to taking "last matched" only

$
0
0

Using jq I am executing this query

{places: [JOIN(INDEX(.thingGroups[]; .thingGroup); .places[]; .thingGroup[] // empty; add)]}

against this data

{"places": [        {"location": "wwc1","thingGroup": ["bs-1"            ]        },        {"location": "wwc2","thingGroup": ["bs-2"            ]        },        {"location": "wwc3","thingGroup": ["bs-3","rs"            ]        },        {"location": "wwc4","thingGroup": ["bs-4","rs"            ]        },        {"location": "wwp1","thingGroup": ["refdoc-bs"            ]        }    ],"thingGroups": [        {"thingGroup": "bs-1","things": ["bp","ci","cc"            ]        },        {"thingGroup": "bs-2","things": ["ems","erec","exs"            ]        },        {"thingGroup": "bs-3","things": ["ci","co","dq"            ]        },        {"thingGroup": "bs-4","things": ["ems","erec","home"            ]        },        {"thingGroup": "rs","things": ["cust"            ]        },        {"thingGroup": "refdoc-bs","things": ["refdoc"            ]        }    ]}

The issue is locations "wwc3" and "wwc4" are only being matched to thingGroup "rs". The desired output is to have the things array generated for these two locations be a combination of all thingGroups that match, not just the last one. Those elements should look like this:

{"location": "wwc3","thingGroup": ["bs-3","rs"    ],"things": ["ci","co","dq","cust"    ]}, {"location": "wwc4","thingGroup": ["bs-4","rs"    ],"things": ["ems","erec","home","cust"    ]}

I have tried using the in() and has() functions to no avail. I feel I am close but missing something obvious. Any help is appreciated.

Demo


Viewing all articles
Browse latest Browse all 524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>