jsonapache-nifijolt

complex jolt transformation - inject into elements in an array


I have complex (EDI) input that looks like this:

{
  "INS-2000_loop": [
    {
      "INS_05": "A",
      "INS_06": "D",
      "INS_08": "AC"
    },
    {
      "REF_01": "0F",
      "REF_02": "111111111"
    },
    {
      "NM1-2100A_loop": [
        {
          "NM1_01": "IL",
          "NM1_02": "1",
          "NM1_03": "aaa",
          "NM1_04": "bbb"
        },
        {
          "PER_03": "HP",
          "PER_04": "1234567890"
        }
      ]
    },
    {
      "HD-2300_loop": [
        {
          "HD_01": "001",
          "HD_03": "HMO",
          "HD_04": "485-014-000",
          "HD_05": "IND"
        },
        {
          "DTP_01": "348",
          "DTP_02": "D8",
          "DTP_03": "20240401"
        }
      ]
    },
    {
      "HD-2300_loop": [
        {
          "HD_01": "001",
          "HD_03": "HMO",
          "HD_04": "485-014-000",
          "HD_05": "IND"
        },
        {
          "DTP_01": "348",
          "DTP_02": "D8",
          "DTP_03": "20240101"
        },
        {
          "DTP_01": "349",
          "DTP_02": "D8",
          "DTP_03": "20240331"
        }
      ]
    }
  ]
}

The input can have any number of HD-2300_loop elements (typically less than 5) and only one INS-2000_loop element. I want the HD-2300_loop elements to "drive" the output so that output is an array of complex objects of the same length as the number of HD-2300_loop elements. The complex objects of the output array need to have all the data in the HD-2300_loop elements and the data of all the children of the INS-2000_loop element except the HD-2300_loop looking like this:

[
    {
        "L2300": [
            {
                "HD_01": "001",
                "HD_03": "HMO",
                "HD_04": "485-014-000",
                "HD_05": "IND"
            },
            {
                "DTP_01": "348",
                "DTP_02": "D8",
                "DTP_03": "20240101"
            },
            {
                "DTP_01": "349",
                "DTP_02": "D8",
                "DTP_03": "20240331"
            }
        ],
        "L2000": [
            {
                "INS_05": "A",
                "INS_06": "D",
                "INS_08": "AC"
            },
            {
                "REF_01": "0F",
                "REF_02": "111111111"
            },
            {
                "NM1-2100A_loop": [
                    {
                        "NM1_01": "IL",
                        "NM1_02": "1",
                        "NM1_03": "aaa",
                        "NM1_04": "bbb"
                    },
                    {
                        "PER_03": "HP",
                        "PER_04": "1234567890"
                    }
                ]
            }
        ]
    },
    ...
]

I've started with the following spec which isn't giving what I want - I haven't figured out how to inject the @3 selection into the result of the @1 selection. Also, I haven't figure out how to rename the selection results (to L2300 and L2000) and how to skip the HD-2300_loop child in the @3 selection result.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "HD-2300_loop": {
            "@1": "",
            "@3": ""
          }
        }
      }
    }
  }
]

I would also greatly appreciate if anyone could point me a Jolt doc or cheat sheet that explains how the & operators work. Thanks very much.


Solution

  • Here's the final solution I came up with (includes some transformations not mentioned in the original problem):

    [
        {
          "operation": "shift",
          "spec": {
            "INS-2000_loop": {
              "*": {
                "INS*": "L2000.&",
                "NM1-2100A_loop": {
                  "*": {
                    "D*|L*|N*": "L2000.L2100A.&",
                    "PER_04": {
                      "@": "L2000.L2100A.PER[].value",
                      "@(1,PER_03)": "L2000.L2100A.PER[#2].type"
                    },
                    "PER_06": {
                      "@": "L2000.L2100A.PER[].value",
                      "@(1,PER_05)": "L2000.L2100A.PER[#2].type"
                    },
                    "PER_08": {
                      "@": "L2000.L2100A.PER[].value",
                      "@(1,PER_07)": "L2000.L2100A.PER[#2].type"
                    }
                  }
                },
                "NM1-2100G_loop": {
                  "*": {
                    "N*": "L2000.L2100G.&",
                    "PER_04": {
                      "@": "L2000.L2100G.PER[].value",
                      "@(1,PER_03)": "L2000.L2100G.PER[#2].type"
                    },
                    "PER_06": {
                      "@": "L2000.L2100G.PER[].value",
                      "@(1,PER_05)": "L2000.L2100G.PER[#2].type"
                    },
                    "PER_08": {
                      "@": "L2000.L2100G.PER[].value",
                      "@(1,PER_07)": "L2000.L2100G.PER[#2].type"
                    }
                  }
                },
                "REF_01": {
                  "*": {
                    "@(2,REF_02)": "L2000.@(2)"
                  }
                },
                "HD-2300_loop": {
                  "*": {
                    "HD*": "L2300s.[&3].L2300.&",
                    "DTP_01": {
                      "*": {
                        "@(2,DTP_03)": "L2300s.[&5].L2300.@(2)"
                      }
                    },
                    "LX-2310_loop": {
                      "*": {
                        "N*": "L2300s.[&5].L2300.L2310.&",
                        "PER_04": {
                          "@": "L2300s.[&6].L2300.L2310.PER[].value",
                          "@(1,PER_03)": "L2300s.[&6].L2300.L2310.PER[#2].type"
                        },
                        "PER_06": {
                          "@": "L2300s.[&6].L2300.L2310.PER[].value",
                          "@(1,PER_05)": "L2300s.[&6].L2300.L2310.PER[#2].type"
                        },
                        "PER_08": {
                          "@": "L2300s.[&6].L2300.L2310.PER[].value",
                          "@(1,PER_07)": "L2300s.[&6].L2300.L2310.PER[#2].type"
                        }
                      }
                    },
                    "COB-2320_loop": {
                      "*": {
                        "COB*": "L2300s.[&5].L2300.L2320[&5].&",
                        "DTP_01": {
                          "*": {
                            "@(2,DTP_03)": "L2300s.[&7].L2300.L2320[&7].@(2)"
                          }
                        },
                        "NM1-2330_loop": {
                          "*": {
                            "N*": "L2300s.[&7].L2300.L2320[&7].L2330.&"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "operation": "shift",
          "spec": {
            "L2000": "&",
            "L2300s": {
              "*": { "L2300": "L2300s[]" }
            }
          }
        },
        {
          "operation": "shift",
          "spec": {
            "L2300s": {
              "*": {
                "@(3,L2000)": "[&].L2000",
                "@": "[&].L2300"
              }
            }
          }
        }
      ]