Search code examples
jsonjolt

jolt transformation for EDI data - array to array


I have EDI data that has been parsed to json that I want to transform. Here is the (simplified) input that contains multiple COB-2320_loop elements:

{
  "INS-2000_loop": [
    {
      "HD-2300_loop": [
        {
          "COB-2320_loop": [
            {
              "COB_01": "P",
              "COB_02": "ABC10XX0092001",
              "COB_03": "1"
            },
            {
              "DTP_01": "344",
              "DTP_02": "D8",
              "DTP_03": "20240101"
            },
            {
              "DTP_01": "345",
              "DTP_02": "D8",
              "DTP_03": "20240101"
            }
          ]
        },
        {
          "COB-2320_loop": [
            {
              "COB_01": "P",
              "COB_02": "ABC10XX0092000",
              "COB_03": "1"
            },
            {
              "DTP_01": "344",
              "DTP_02": "D8",
              "DTP_03": "20230101"
            },
            {
              "DTP_01": "345",
              "DTP_02": "D8",
              "DTP_03": "20230101"
            }
          ]
        },
        {
          "COB-2320_loop": [
            {
              "COB_01": "P",
              "COB_02": "ABC10XX00920",
              "COB_03": "1"
            },
            {
              "DTP_01": "344",
              "DTP_02": "D8",
              "DTP_03": "20220101"
            },
            {
              "DTP_01": "345",
              "DTP_02": "D8",
              "DTP_03": "20220101"
            }
          ]
        }
      ]
    }
  ]
}

Here is the transform spec I'm currently using:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "HD-2300_loop": {
            "*": {
              "COB-2320_loop": {
                "*": {
                  "COB_01": "&6[&5].coverages.cobs.[#].responsibility",
                  "COB_02": "&6[&5].coverages.cobs.[#].id",
                  "COB_03": "&6[&5].coverages.cobs.[#].code",
                  "DTP_01": {
                    "344": {
                      "@(2,DTP_03)": "&8[&7].coverages.cobs.[#].start"
                    },
                    "345": {
                      "@(2,DTP_03)": "&8[&7].coverages.cobs.[#].end"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

This transform results (incorrectly) in:

{
  "INS-2000_loop" : [ {
    "coverages" : {
      "cobs" : [ {
        "responsibility" : [ "P", "P", "P" ],
        "id" : [ "ABC10XX0092001", "ABC10XX0092000", "ABC10XX00920" ],
        "code" : [ "1", "1", "1" ],
        "start" : [ "20240101", "20230101", "20220101" ],
        "end" : [ "20240101", "20230101", "20220101" ]
      } ]
    }
  } ]
}

I want the resulting cobs array to look like this:

{
  "coverages": {
    "cobs": [
      {
        "responsibility": "P",
        "id": "ABC10XX00920",
        "code": "1",
        "start": "20240101",
        "end": "20240101"
      },
      {
        "responsibility": "P",
        "id": "ABC10XX0092001",
        "code": "1",
        "start": "20240101",
        "end": "20240101"
      },
      {
        "responsibility": "P",
        "id": "ABC10XX0092001",
        "code": "1",
        "start": "20240101",
        "end": "20240101"
      }
    ]
  }
}

Also, the source data may zero or more COB-2320_loop objects. Any help is greatly appreciated.


Solution

  • So far so good, just need to separate the objects by the indexes of the "HD-2300_loop" array, those have values 0,1,2 in arraywise manner (eg. by using [&3] and [&5] to be able to reach the level of those indexes) such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "HD-2300_loop": {
                "*": {
                  "COB-2320_loop": {
                    "*": {
                      "COB_01": "&6[&5].coverages.cobs[&3].responsibility",
                      "COB_02": "&6[&5].coverages.cobs[&3].id",
                      "COB_03": "&6[&5].coverages.cobs[&3].code",
                      "DTP_01": {
                        "344": {
                          "@(2,DTP_03)": "&8[&7].coverages.cobs[&5].start"
                        },
                        "345": {
                          "@(2,DTP_03)": "&8[&7].coverages.cobs[&5].end"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]