arraysjsonjolt

Jolt transformation to remove nested array


Could you please assist me in performing a Jolt transformation to convert the JSON structure provided below into the desired output format? I would greatly appreciate your guidance or a sample transformation specification that achieves this conversion. Thank you! From JSON :

[
  {
    "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399",
    "items": [
      {
        "item_id": "00-00002475",
        "type_of_goods": "RR"
      },
      {
        "item_id": "00-00001243",
        "type_of_goods": "TT"
      },
      {
        "item_id": "00-00002997",
        "type_of_goods": "AA"
      }
    ]
  },
  {
    "doc_GUID": "e83f9131-a034-11ef-aaeb-ec2a723f4399",
    "items": [
      {
        "item_id": "00-00002828",
        "type_of_goods": "YY"
      },
      {
        "item_id": "00-000029971",
        "type_of_goods": "EE"
      }
    ]
  }
]

To this result:


[
  {
    "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399",
    "item_id": "00-00002475",
    "type_of_goods": "RR"
  },
  {
    "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399",
    "item_id": "00-00001243",
    "type_of_goods": "TT"
  },
  {
    "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399",
    "item_id": "00-00002997",
    "type_of_goods": "AA"
  },
  {
    "doc_GUID": "e83f9131-a034-11ef-aaeb-ec2a723f4399",
    "item_id": "00-00002828",
    "type_of_goods": "YY"
  },
  {
    "doc_GUID": "e83f9131-a034-11ef-aaeb-ec2a723f4399",
    "item_id": "00-000029971",
    "type_of_goods": "EE"
  }
]

Solution

  • You can loop within the objects of the items array while bringing the doc_GUID attribute from two upper level such as :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "items": {
              "*": {
                "@2,doc_GUID": "&3_&1.doc_GUID",
                "*": "&3_&1.&"
              }
            }
          }
        }
      },
      { //get rid of the object keys
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    enter image description here