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

Merging two arrays using Jq command

$
0
0

I have the following JSON object that I want to merge. So the scenario is ticketId and requester_id should become an attribute of each objects in componentsByProdTourId array.Here is the input JSON data.

{"componentsByProdTourId": [    {"region": "Europe","compSpec": {"country": "France","attributes": [          {"attributeCode": "ABC","attributeType": "xyx"          }        ]      }    },    {"region": "Europe","compSpec": {"country": "France","attributes": [          {"attributeCode": "EFG","attributeType": "lmn"          }        ]      }    },    {"region": "Europe","compSpec": {"country": "United Kingdom","attributes": [          {"attributeCode": "FLC","attributeType": "omp"          }        ]      }    }  ],"ticketData": {"ticketId": "1234","requester_id": "99885"  }}

The desired out I am looking for:

{"componentsByProdTourId": [    {"region": "Europe","compSpec": {"country": "France","attributes": [          {"attributeCode": "ABC","attributeType": "xyx"          }        ]      },"ticketId": 1234,"requester_id": 99885    },    {"region": "Europe","compSpec": {"country": "France","attributes": [          {"attributeCode": "EFG","attributeType": "lmn"          }        ]      },"ticketId": 1234,"requester_id": 99885    },    {"region": "Europe","compSpec": {"country": "United Kingdom","attributes": [          {"attributeCode": "FLC","attributeType": "omp"          }        ]      },"ticketId": 1234,"requester_id": 99885    }  ],"ticketData": {"ticketId": "1234","requester_id": "99885"  }}

Thanks in advance.

I tried this Jq command:

.componentsByProdTourId |= map(. + {ticketId: .ticketData.ticketId, requester_id: .ticketData.requester_id})

But the result was not ideal. ticketId and requester_id were null. Here is the output:

{"componentsByProdTourId": [    {"region": "Europe","compSpec": {"country": "France","attributes": [          {"attributeCode": "ABC","attributeType": "xyx"          }        ]      },"ticketId": null,"requester_id": null    },    {"region": "Europe","compSpec": {"country": "France","attributes": [          {"attributeCode": "EFG","attributeType": "lmn"          }        ]      },"ticketId": null,"requester_id": null    },    {"region": "Europe","compSpec": {"country": "United Kingdom","attributes": [          {"attributeCode": "FLC","attributeType": "omp"          }        ]      },"ticketId": null,"requester_id": null    }  ],"ticketData": {"ticketId": "1234","requester_id": "99885"  }}

Viewing all articles
Browse latest Browse all 527

Trending Articles



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