skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

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

  2. Hi this spec will help you resolve the query :

    [
      {
        "operation": "shift",
        "spec": {
          "INS-2000_loop": {
            "*": {
              "HD-2300_loop": {
                "*": "[#5].L2300"
              },
              "INS_*": "[#4].L2000.[#2].&",
              "REF_*": "[#4].L2000.[#2].&",
              "NM1-2100A_loop": "[#4].L2000.[#2].&"
            }
          }
        }
      }, {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "L2300": {
              "*": {
                "key": "=concat(@(1,HD_01),@(1,HD_03),@(1,HD_04),@(1,HD_05),@(1,DTP_01),@(1,DTP_02),@(1,DTP_03))"
              }
            }
          }
        }
      }, {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&",
            "L2300": {
              "*": "test.@key[]"
            }
          }
        }
      }, {
        "operation": "cardinality",
        "spec": {
          "test": {
            "*": "ONE"
          }
        }
      }, {
        "operation": "shift",
        "spec": {
          "*": "[#2].&",
          "test": {
            "*": {
              "@": "[#4].L2300.[]"
            }
          }
        }
      }, {
        "operation": "remove",
        "spec": {
          "*": {
            "*": {
              "*": {
                "key": ""
              }
            }
          }
        }
      }
    ]
    

    Explaination:

    As for am keeping the things as normal required list.

      {
        "operation": "shift",
        "spec": {
          "INS-2000_loop": {
            "*": {
              "HD-2300_loop": {
                "*": "[#5].L2300"
              },
              "INS_*": "[#4].L2000.[#2].&",
              "REF_*": "[#4].L2000.[#2].&",
              "NM1-2100A_loop": "[#4].L2000.[#2].&"
            }
          }
        }
      }
    
    1. In first operation:

    From "HD-2300_loop" am taking all attributes and keeping them in new array "L2300" and rest fields in "L2000".

     {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "L2300": {
              "*": {
                "key": "=concat(@(1,HD_01),@(1,HD_03),@(1,HD_04),@(1,HD_05),@(1,DTP_01),@(1,DTP_02),@(1,DTP_03))"
              }
            }
          }
        }
      }
    
    1. In this operation am creatin a unique key based on attributes present in each L2300 object to eliminate duplicates.

      {
      "operation": "shift",
      "spec": {
      "": {
      "
      ": "&",
      "L2300": {
      "": "test.@key[]"
      }
      }
      }
      }, {
      "operation": "cardinality",
      "spec": {
      "test": {
      "
      ": "ONE"
      }
      }
      }

    2. This 3 specs are used to remove dublicate attributes from the L2300 list.

      {
      "operation": "shift",
      "spec": {
      "": "[#2].&",
      "test": {
      "
      ": {
      "@": "[#4].L2300.[]"
      }
      }
      }
      }, {
      "operation": "remove",
      "spec": {
      "": {
      "
      ": {
      "*": {
      "key": ""
      }
      }
      }
      }
      }

    3. Last 2 operations are to create required structure.

    For Jolt Documentation of # and & you can explore Digibee documentations :

    https://docs.digibee.com/documentation/components/tools/transformer-jolt/transformer-getting-to-know-jolt

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search