Home > Enterprise >  Jolt Transform to append extracted data
Jolt Transform to append extracted data

Time:02-05

I'm new to Jolt Transform and have some EDI data that I want to transform. I want to keep the original data and append a few extracted elements in order to make it easier for a downstream process to get to. Here's a sample:

Input

{
  "id": "2000",
  "segments": [
    {
      "REF02": "xxxxxxxxxxxx",
      "REF01": "0F",
      "id": "REF"
    },
    {
      "REF02": "yyyyyyyyyyyy",
      "REF01": "1L",
      "id": "REF"
    }
  ],
  "loops": [
    {
      "id": "2100",
      "segments": []
    },
    {
      "id": "2100",
      "segments": [],
      "loops": [
        {
          "id": "2300",
          "segments": [
            {
              "DTP01": "348",
              "DTP03": "20220101",
              "DTP02": "D8",
              "id": "DTP"
            },
            {
              "DTP01": "349",
              "DTP03": "20221231",
              "DTP02": "D8",
              "id": "DTP"
            }
          ]
        }
      ]
    }
  ]
}

Desired Output

{
  "ext": {
    "subscriber": "xxxxxxxxxxxx",
    "start": "20220101"
  },
  "id": "2000",
  "segments": [
    {
      "REF02": "xxxxxxxxxxxx",
      "REF01": "0F",
      "id": "REF"
    },
    {
      "REF02": "yyyyyyyyyyyy",
      "REF01": "1L",
      "id": "REF"
    }
  ],
  "loops": [
    {
      "id": "2100",
      "segments": []
    },
    {
      "id": "2100",
      "segments": [],
      "loops": [
        {
          "id": "2300",
          "segments": [
            {
              "DTP01": "348",
              "DTP03": "20220101",
              "DTP02": "D8",
              "id": "DTP"
            },
            {
              "DTP01": "349",
              "DTP03": "20221231",
              "DTP02": "D8",
              "id": "DTP"
            }
          ]
        }
      ]
    }
  ]
}

That is, I'd like to append the ext element - copying values from the original.

In xpath terms, the extracted values' paths look something like this:

subscriber :

 /segments[../id eq "2000"]/..[REF01 eq "0F"]/REF02/text()

start :

 /loops[../id eq "2000"]/../segments[../id eq "2100"])[1]/..
          [id eq "NM1" and NM101 eq "IL"][DTP01 eq "348"]/DTP03/text()

Thanks for the help.

CodePudding user response:

I ended up with this solution. If there's any feedback on streamlining, making more elegant, let me know.

[
  {
    "operation": "default",
    "spec": {
      "dummy": {}
    }
  },
  {
    "operation": "shift",
    "spec": {
      "dummy": {
        "@2,segments": {
          "*": {
            "REF01": {
              "0F": {
                "@2,REF02": "ext.subscriber"
              }
            }
          }
        },
        "@2,loops": {
          "*": {
            "id": {
              "2100": {
                "@2,loops": {
                  "*": {
                    "id": {
                      "2300": {
                        "@2,segments": {
                          "*": {
                            "DTP01": {
                              "348": {
                                "@2,DTP03": "ext.start"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "id": "id",
      "segments": "segments",
      "loops": "loops"
    }
  }
]

CodePudding user response:

You can replicate the whole content by use of "@": "" within a shift transformation while adding less curly braces to nest the inner stuff, at the same time determining the paths to search to be used in the next spec such as

[
  {
    "operation": "shift",
    "spec": {
      "@": "",
      "segments": {
        "*": {//indexes of the array
          "@REF02": "ext.su.@REF01"
        }
      },
      "loops": {
        "*": {//indexes of the array
          "@loops": {
            "*": {//indexes of the array
              "@segments": {
                "*": {//indexes of the array
                  "@DTP03": "ext.st.@5,id.@3,id.@DTP01"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "ext": {
        "@su.0F": "&1.subscriber",
        "@st.2100.2300.348": "&1.start"
      },
      "*": "&" //replicate the other attributes/arrays/objects
    }
  }
]
  • Related