Merging two arrays using Jq command

48 views Asked by At

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"
  }
}
1

There are 1 answers

2
hobbs On
.componentsByProdTourId[] += .ticketData

is enough. Your version is failing because at the time when you try to access .ticketData, . is the item you're mapping over, and not the root object. You could fix that by using as to capture .ticketData, but there's an easier way which is to skip map entirely.