jsonjolt

Need jolt spec to give combinations


input :

{
  "values": [
    {
      "reference_id": "TEST_51_ST",
      "idoc_number": "0000000113916030",
      "sto_number": "",
      "sending_location": "0000000625",
      "receiving_location": "4955",
      "idoc_status": "51",
      "create_time": "07:19:34",
      "create_date": "2023-07-20",
      "values1": [
        {
          "material_number": "A17265420",
          "status": "E",
          "message_class": "06",
          "message_num": "215",
          "message_text": "Please enter net price"
        },
        {
          "material_number": "",
          "status": "E",
          "message_class": "BAPI",
          "message_num": "001",
          "message_text": "No instance of object type PurchaseOrder has been created. External reference:"
        }
      ]
    },
    {
      "reference_id": "TEST_51_ST123",
      "idoc_number": "0000000113916030312",
      "sto_number": "",
      "sending_location": "00000006253",
      "receiving_location": "49553",
      "idoc_status": "513",
      "create_time": "07:19:34",
      "create_date": "2023-07-20",
      "values1": [
        {
          "material_number": "A172654203",
          "status": "E",
          "message_class": "06",
          "message_num": "2153",
          "message_text": "Please enter net price"
        },
        {
          "material_number": "",
          "status": "E",
          "message_class": "BAPI",
          "message_num": "0031",
          "message_text": "No instance of object type PurchaseOrder has been created. External reference:"
        }
      ]
    }
  ]
}

expected output :

[
  {
    "reference_id": "TEST_51_ST",
    "idoc_number": "0000000113916030",
    "sto_number": "",
    "sending_location": "0000000625",
    "receiving_location": "4955",
    "idoc_status": "51",
    "create_time": "07:19:34",
    "create_date": "2023-07-20",
    "material_number": "A17265420",
    "status": "E",
    "message_class": "06",
    "message_num": "215",
    "message_text": "Please enter net price"
  },
  {
    "reference_id": "TEST_51_ST",
    "idoc_number": "0000000113916030",
    "sto_number": "",
    "sending_location": "0000000625",
    "receiving_location": "4955",
    "idoc_status": "51",
    "create_time": "07:19:34",
    "create_date": "2023-07-20",
    "material_number": "",
    "status": "E",
    "message_class": "BAPI",
    "message_num": "001",
    "message_text": "No instance of object type PurchaseOrder has been created. External reference:"
  },
  {
    "reference_id": "TEST_51_ST123",
    "idoc_number": "0000000113916030312",
    "sto_number": "",
    "sending_location": "00000006253",
    "receiving_location": "49553",
    "idoc_status": "513",
    "create_time": "07:19:34",
    "create_date": "2023-07-20",
    "material_number": "A172654203",
    "status": "E",
    "message_class": "06",
    "message_num": "2153",
    "message_text": "Please enter net price"
  },
  {
    "reference_id": "TEST_51_ST123",
    "idoc_number": "0000000113916030312",
    "sto_number": "",
    "sending_location": "00000006253",
    "receiving_location": "49553",
    "idoc_status": "513",
    "create_time": "07:19:34",
    "create_date": "2023-07-20",
    "material_number": "",
    "status": "E",
    "message_class": "BAPI",
    "message_num": "0031",
    "message_text": "No instance of object type PurchaseOrder has been created. External reference:"
  }
]

need a jolt spec to solve this tried with this jolt but it is working for only one array element

[
  {
    "operation": "shift",
    "spec": {
      "values": {
        "*": {
          "values1": {
            "*": {
              // Extract fields from the main object along with each `values1` item
              "@(2,reference_id)": "[&1].reference_id",
              "@(2,idoc_number)": "[&1].idoc_number",
              "@(2,sto_number)": "[&1].sto_number",
              "@(2,sending_location)": "[&1].sending_location",
              "@(2,receiving_location)": "[&1].receiving_location",
              "@(2,idoc_status)": "[&1].idoc_status",
              "@(2,create_time)": "[&1].create_time",
              "@(2,create_date)": "[&1].create_date",
              // Map each field within `values1`
              "material_number": "[&1].material_number",
              "status": "[&1].status",
              "message_class": "[&1].message_class",
              "message_num": "[&1].message_num",
              "message_text": "[&1].message_text"
            }
          }
        }
      }
    }
  }
]

but it is not working properly.It is working for this kind of payload Direct Parent Referencing (@(3, ...)): Each parent field outside values1 (like reference_id, idoc_number, etc.) is accessed at level 3 (relative to values1), ensuring that each values1 item generates its own object with a unique set of parent values. Unique Flattened Entries ([&2]): The values1 array elements are each flattened into distinct objects, so the output JSON has independent entries for each values1 element, without grouping fields into arrays. Expected Output This should generate a list of distinct objects with fields like reference_id, idoc_number, material_number, and others for each values1 entry, without combining values into arrays. Let me know if this provides the required format


Solution

  • No need to write the pairs one by one, even for the ones which are at outside of the "values1" arrays while keeping on looping through those arrays such as

    [
      {//accumulate the attributes, which stay out of the "values1" arrays, within a common object
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "[&1].Others.&",
              "values1": {
                "*": {
                  "*": "[&3].&2[&1].&"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "values1": {
              "*": {
                "@2,Others": { "*": "&4_&1.&" },
                "*": "&3_&1.&"
              }
            }
          }
        }
      },
      {//get rid of the object keys
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]