Support Questions

Find answers, ask questions, and share your expertise

JOLT Trasform - Help Needed

avatar
Contributor

Hi community,

i'm fighting with my JOLT transform, hope someone can help me on this.

I have the following json flow file:

[
  {
    "StartName": "<My_Start_Name_1>",
    "StartAddress": "<My_Start_Address_1>",
    "EndName": "<My_End_Name_1>",
    "EndAddress": "My_End_Address_1",
    "CubeSped": 0.58,
    "WeightSped": 36
  },
  {
    "StartName": "<My_Start_Name_2>",
    "StartAddress": "<My_Start_Address_2>",
    "EndName": "<My_End_Name_2>",
    "EndAddress": "My_End_Address_2",
    "CubeSped": 0.88,
    "WeightSped": 22
  }
]

My expected result should be:

{
    "Data": {
      "MessageId": "<My_Random_ID>",
      "MessageDate": "<My_Current_Date>",
      "Client": "<My_Customer>",
      "Order": [
        {
          "ProcessType": {
            "ReturnOrder": {
            }
          },
          "FreightTerm": "PointOfUse",
          "OrderNumber": "123456789160X",
          "OrderNumber2": "123456789160X",
          "TransmissionCommand": "Create",
          "SequenceNumber": "1",
          "SenderAbbreviation": "Test",
          "CustomerAddress": {
            "Name": "<My_Start_Name_1>",
            "Street": "<My_Start_Address_1>"
          },
          "ShipmentAddress": {
            "Name": "<My_End_Name_1>",
            "Street": "<My_End_Address_1>"
          },
          "Volume": "1",
          "Weight": "365"
        },
        {
            "ProcessType": {
                "ReturnOrder": {
                }
            },
            "FreightTerm": "PointOfUse",
            "OrderNumber": "123456789160X",
            "OrderNumber2": "123456789160X",
            "TransmissionCommand": "Create",
            "SequenceNumber": "1",
            "SenderAbbreviation": "Test",
            "CustomerAddress": {
                "Name": "<My_Start_Name_2>",
                "Street": "<My_Start_Address_2>"
            },
            "ShipmentAddress": {
                "Name": "<My_End_Name_2>",
                "Street": "<My_End_Address_2>"
            },
            "Volume": "1",
            "Weight": "365"
            }
        ]
    }
}

 Currently my spec are:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "CubeSped": "Order[&1].Volume",
        "WeightSped": "Order[&1].Weight",
        "DocNumLDC": "Order[&1].OrderNumber",
        "StartName": "Order[&1].CustomerAddress.Name",
        "StartAddress": "Order[&1].CustomerAddress.Street",
        "EndName": "Order[&1].ShipmentAddress.Name",
        "EndAddress": "Order[&1].ShipmentAddress.Street"
      }
    }
  }, {
    "operation": "default",
    "spec": {
      "Order[]": {
        "*": {
          "top-level": {
            "ProcessType": {
              "ReturnOrder": ""
            },
            "FreightTerm": "PointOfUse",
            "OrderNumber2": "123456789160X",
            "TransmissionCommand": "Create",
            "SequenceNumber": "1",
            "SenderAbbreviation": "Test"
          }
        }
      }
    }
  }
]

Which becomes:

{
  "Order" : [ {
    "Volume" : 0.58,
    "Weight" : 36,
    "CustomerAddress" : {
      "Name" : "<My_Start_Name_1>",
      "Street" : "<My_Start_Address_1>"
    },
    "ShipmentAddress" : {
      "Name" : "<My_End_Name_1>",
      "Street" : "My_End_Address_1"
    },
    "top-level" : {
      "TransmissionCommand" : "Create",
      "ProcessType" : {
        "ReturnOrder" : ""
      },
      "FreightTerm" : "PointOfUse",
      "SequenceNumber" : "1",
      "SenderAbbreviation" : "Test",
      "OrderNumber2" : "123456789160X"
    }
  }, {
    "Volume" : 0.88,
    "Weight" : 22,
    "CustomerAddress" : {
      "Name" : "<My_Start_Name_2>",
      "Street" : "<My_Start_Address_2>"
    },
    "ShipmentAddress" : {
      "Name" : "<My_End_Name_2>",
      "Street" : "My_End_Address_2"
    },
    "top-level" : {
      "TransmissionCommand" : "Create",
      "ProcessType" : {
        "ReturnOrder" : ""
      },
      "FreightTerm" : "PointOfUse",
      "SequenceNumber" : "1",
      "SenderAbbreviation" : "Test",
      "OrderNumber2" : "123456789160X"
    }
  } ]
}

The point is:

  • is there any way to move elements on top?
  • is there a better way to achieve the result?
  • Does anything else can be used instead of JOLT?

The result schema is needed after in order to covert everything into xml.

Many thanks for any help, appreciate.

1 REPLY 1

avatar
Explorer

Hey,

Generally JOLT tries to maintain the input order. The only way that I can think about is an hard-coded approach.

Add this after the default spec:

{
    "operation": "shift",
    "spec": {
      "Order": {
        "*": {
          "top-level": {
            "ProcessType": "[&2].ProcessType",
            "FreightTerm": "[&2].FreightTerm",
            "OrderNumber2": "[&2].OrderNumber2",
            "TransmissionCommand": "[&2].TransmissionCommand",
            "SequenceNumber": "[&2].SequenceNumber",
            "SenderAbbreviation": "[&2].SenderAbbreviation"
          },
          "CustomerAddress": "[&1].CustomerAddress",
          "ShipmentAddress": "[&1].ShipmentAdress",
          "Volume": "[&1].Volume",
          "Weight": "[&1].Weight"
        }
      }
    }
  }

 

Also, are you trying to "duplicate" the value of "OrderNumber2"?

 

The other solution I can remember to "replace" JOLT is by using FasterXML/Jackson