Support Questions

Find answers, ask questions, and share your expertise

Going Crazy with JOLT

avatar
Contributor

Sometimes you may think you understand well about JOLT....then you stuck with another new transformation... 😉

My data:

{
  "Orders": [
    {
      "Headers": {
        "UniqShipment": "0",
        "OCCustomer": "V2472-0100",
        "erpcode": "C001728"
      },
      "Locations": {
        "Shipper": {
          "SenderAddress": "address 1"
        },
        "Load": {
          "LoadAddress": "address 2",
        },
        "Consignee": {
          "ConsigneeName": "consignee name"
        },
        "Unload": {
          "UnloadAddress": "address 4"
        }
      },
      "Goods": [
        {
          "GoodsTypeID": 3,
          "GoodsTypeName": "good name",
          "GoodsDetails": [
            {
              "Packs": 1,
              "NetWeight": 3.800000,
              "GrossWeight": 4.800000,
              "Cube": 0.693000,
              "Meters": 0.000000,
              "Note": "good note"
            }
          ]
        }
      ],
      "References": [
        {
          "TypeReference": "DT",
          "ValueReference": "010130",
          "DateReference": "2023-06-20"
        },
        {
          "TypeReference": "CM",
          "ValueReference": "30492 STOCK-SP"
        },
        {
          "TypeReference": "OC",
          "ValueReference": "V2472-0100"
        },
        {
          "TypeReference": "RG",
          "ValueReference": "reference value"
        }
      ]
    },
      {
      "Headers": {
        "UniqShipment": "0",
        "OCCustomer": "V2472-0100",
        "erpcode": "C001728"
      },
      "Locations": {
        "Shipper": {
          "SenderAddress": "address 1"
        },
        "Load": {
          "LoadAddress": "address 2",
        },
        "Consignee": {
          "ConsigneeName": "consignee name"
        },
        "Unload": {
          "UnloadAddress": "address 4"
        }
      },
      "Goods": [
        {
          "GoodsTypeID": 3,
          "GoodsTypeName": "good name",
          "GoodsDetails": [
            {
              "Packs": 1,
              "NetWeight": 3.800000,
              "GrossWeight": 4.800000,
              "Cube": 0.693000,
              "Meters": 0.000000,
              "Note": "good note"
            }
          ]
        }
      ],
      "References": [
        {
          "TypeReference": "DT",
          "ValueReference": "010130",
          "DateReference": "2023-06-20"
        },
        {
          "TypeReference": "CM",
          "ValueReference": "30492 STOCK-SP"
        },
        {
          "TypeReference": "OC",
          "ValueReference": "V2472-0100"
        },
        {
          "TypeReference": "RG",
          "ValueReference": "reference value"
        }
      ]
    }
  ]
}

My Transform:

  [{
  	"operation": "shift",
  	"spec": {
  		"Orders": {
  			"*": {
  				"Headers": {
  					"*": "header.&"
  				},
  				"Locations": {
  					"Shipper": {
  						"*": "sender.&"
  					},
  					"Consignee": {
  						"*": "consignee.&"
  					},
  					"Unload": {
  						"*": "unload.&"
  					}
  				},
  				"Goods": {
  					"*": {
  						"GoodsDetails": {
  							"*": {
  								"@(2,GoodsTypeName)": "rows[].GoodsTypeName",
  								"@(4,Locations.Consignee.ConsigneeName)": "rows[].ConsigneeName"
  							}
  						}
  					}
  				}
  			}
  		}

  	}
  }, {
  	"operation": "cardinality",
  	"spec": {
  		"header": {
  			"*": "ONE"
  		},
  		"sender": {
  			"*": "ONE"
  		},
  		"consignee": {
  			"*": "ONE"
  		},
  		"unload": {
  			"*": "ONE"
  		}
  	}
  }]

My Output:

{
	"header": {
		"UniqShipment": "0",
		"OCCustomer": "V2472-0100",
		"erpcode": "C001728"
	},
	"sender": {
		"SenderAddress": "address 1"
	},
	"consignee": {
		"ConsigneeName": "consignee name"
	},
	"unload": {
		"UnloadAddress": "address 2",
	},
	"rows": [{
		"GoodsTypeName": "good name"
	}, {
		"ConsigneeName": "consignee name"
	}, {
		"GoodsTypeName": "good name"
	}, {
		"ConsigneeName": "consignee name"
	}, {
		"GoodsTypeName": "good name"
	}, {
		"ConsigneeName": "consignee name 2"
	}, {
		"GoodsTypeName": "good name"
	}, {
		"ConsigneeName": "consignee name 2"
	}]
}

 

My Expected Result:

{
	"header": {
		"UniqShipment": "0",
		"OCCustomer": "V2472-0100",
		"erpcode": "C001728"
	},
	"sender": {
		"SenderAddress": "address 1"
	},
	"consignee": {
		"ConsigneeName": "consignee name"
	},
	"unload": {
		"UnloadAddress": "address 2",
	},
	"rows": [{
		"GoodsTypeName": "good name",
		"ConsigneeName": "consignee name"
	},{
		"GoodsTypeName": "good name",
		"ConsigneeName": "consignee name"
	},{
		"GoodsTypeName": "good name",
		"ConsigneeName": "consignee name"
	},{
		"GoodsTypeName": "good name",
		"ConsigneeName": "consignee name"
	},{
		"GoodsTypeName": "good name",
		"ConsigneeName": "consignee name"
	},{
		"GoodsTypeName": "good name",
		"ConsigneeName": "consignee name"
	}
	]
}

 

What i've missing?

Help appreciate!

1 REPLY 1

avatar
Contributor

Some progress:

I've modified the transformation like this:

  [{
  	"operation": "shift",
  	"spec": {
  		"Orders": {
  			"*": {
  				"Headers": {
  					"*": "header.&"
  				},
  				"Locations": {
  					"Shipper": {
  						"*": "sender.&"
  					},
  					"Consignee": {
  						"*": "consignee.&"
  					},
  					"Unload": {
  						"*": "unload.&"
  					}
  				},
  				"Goods": {
  					"*": {
  						"GoodsDetails": {
  							"*": {
  								"@(2,GoodsTypeName)": "rows.GoodsTypeName",
  								"@(4,Locations.Consignee.ConsigneeName)": "rows.ConsigneeName",
  								"@(4,Locations.Consignee.IdPlanToLocCUS)": "rows.IdPlanToLocCUS",
  								"@(4,Locations.Consignee.ConsigneeAddress)": "rows.ConsigneeAddress",
  								"@(4,Locations.Consignee.ConsigneeCountry)": "rows.ConsigneeCountry",
  								"@(4,Locations.Consignee.ConsigneeEmail)": "rows.ConsigneeEmail",
  								"@(4,Locations.Consignee.ConsigneeNotes)": "rows.ConsigneeNotes",
  								"@(4,Locations.Consignee.ConsigneeReference)": "rows.ConsigneeReference",
  								"@(4,Locations.Consignee.ConsigneeRegion)": "rows.ConsigneeRegion",
  								"@(4,Locations.Consignee.ConsigneeTel)": "rows.ConsigneeTel",
  								"@(4,Locations.Consignee.ConsigneeTown)": "rows.ConsigneeTown",
  								"@(4,Locations.Consignee.ConsigneeZipCode)": "rows.ConsigneeZipCode",
  								"Packs": "rows.Packs",
  								"NetWeight": "rows.NetWeight",
  								"GrossWeight": "rows.GrossWeight",
  								"Cube": "rows.Cube",
  								"Meters": "rows.Meters",
  								"CodiceUnivocoCollo_1": "rows.CodiceUnivocoCollo_1",
  								"CodiceMaster": "rows.CodiceMaster",
  								"ItemCode": "rows.ItemCode",
  								"Seats": "rows.Seats",
  								"Height": "rows.Height",
  								"Width": "rows.Width",
  								"Depth": "rows.Depth",
  								"Note": "rows.Note",
  								"@(4,Locations.Unload.UnloadName)": "rows.UnloadName",
  								"@(4,Locations.Unload.IdShipToLocCUS)": "rows.IdShipToLocCUS",
  								"@(4,Locations.Unload.UnloadAddress)": "rows.UnloadAddress",
  								"@(4,Locations.Unload.UnloadCalendarNote)": "rows.UnloadCalendarNote",
  								"@(4,Locations.Unload.UnloadCountry)": "rows.UnloadCountry",
  								"@(4,Locations.Unload.UnloadDate)": "rows.UnloadDate",
  								"@(4,Locations.Unload.UnloadEmail)": "rows.UnloadEmail",
  								"@(4,Locations.Unload.UnloadNotes)": "rows.UnloadNotes",
  								"@(4,Locations.Unload.UnloadReference)": "rows.UnloadReference",
  								"@(4,Locations.Unload.UnloadRegion)": "rows.UnloadRegion",
  								"@(4,Locations.Unload.UnloadTel)": "rows.UnloadTel",
  								"@(4,Locations.Unload.UnloadTime)": "rows.UnloadTime",
  								"@(4,Locations.Unload.UnloadTown)": "rows.UnloadTown",
  								"@(4,Locations.Unload.UnloadZipCode)": "rows.UnloadZipCode",
  								"@(4,Locations.Unload.ObbligatoryUnloadDate)": "rows.ObbligatoryUnloadDate",
  								"@(4,References)": {
  									"*": {
  										"TypeReference": {
  											"OC": {
  												"@(2,ValueReference)": "rows.info1[]"
  											},
  											"CM": {
  												"@(2,ValueReference)": "rows.info7[]"
  											}
  										}
  									}
  								}
  							}
  						}
  					}
  				}
  			}
  		}

  	}
  }, {
  	"operation": "cardinality",
  	"spec": {
  		"header": {
  			"*": "ONE"
  		},
  		"sender": {
  			"*": "ONE"
  		},
  		"consignee": {
  			"*": "ONE"
  		},
  		"unload": {
  			"*": "ONE"
  		}
  	}
  }, {
  	"operation": "shift",
  	"spec": {
      "header": {
  			"*": "header.&"
  		},
  		"sender": {
  			"*": "sender.&"
  		},
  		"consignee": {
  			"*": "consignee.&"
  		},
  		"unload": {
  			"*": "unload.&"
  		},
  		"rows": {
          "*" : {
            "*" : {
  			"@": "rows[&1].&2"
  		}
         
          }
        }
  	}
  }]

 

Everything works as expected when i have multiple elements in GoodDetails like this:

"GoodsDetails": [
            {
              "Packs": 1,
              "NetWeight": 3.800000,
              "GrossWeight": 4.800000,
              "Cube": 0.693000,
              "Meters": 0.000000,
              "Note": "good note"
            },
{
              "Packs": 1,
              "NetWeight": 3.800000,
              "GrossWeight": 4.800000,
              "Cube": 0.693000,
              "Meters": 0.000000,
              "Note": "good note"
            }
          ]

 

However, when the element is only one like this:

{
              "Packs": 1,
              "NetWeight": 3.800000,
              "GrossWeight": 4.800000,
              "Cube": 0.693000,
              "Meters": 0.000000,
              "Note": "good note"
            }

The result is really bad:

rows": [{
		"ConsigneeZipCode": null,
		"UnloadZipCode": null,
		"info7": "230500003530",
		"info1": "V6157-0360",
		"Seats": null
	}, {
		"Packs": null
	}]

It seems that when the element is only one, the first shift transformation does not consider resulting rows[

{},

{}

]

as an array....

but something like:

row{

name1: value1

name1: value2

...

}

 

Any suggestion really appreciate.