I was using JQPlay to play around the format. I am not able to understand how to use reduce to group by child structure. I want to group based on the org or the parent id.
just update my jqplay filter, but not able to remove two tags to group by id.
jq play syntax-
I am using below syntax in jqplay.org. Also can you please advise how debug anything after pipe symbol.
.items | {"org" : map( {id : .org, orgProperties : [{"properties" : {"methodId" : [{"id" : .methodId}]}}]})| group_by(.id) | map( reduce .[] as $x (.[0]|{}; .orgProperties+= ($x | .orgProperties)))}
Input JSON
{
"items": [
{
"org": "750141",
"methodId": "1-10F7IAK7"
},
{
"org": "750141",
"methodId": "1-10TP18L0"
},
{
"org": "750142",
"methodId": "1-10TP18L1"
}
]
}
Output JSON
{
"org": [
{
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10F7IAK7"
}
]
}
},
{
"properties": {
"methodId": [
{
"id": "1-10TP18L0"
}
]
}
}
]
},
{
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10TP18L1"
}
]
}
}
]
}
]
}
Expected JSON Output
{
"org": [
{
"id": "750141",
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10F7IAK7"
},
{
"id": "1-10TP18L0"
}
]
}
}
]
},
{
"id": "750142",
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10TP18L1"
}
]
}
}
]
}
]
}