This is an input that needs to be transformed using Jolt Transformation to obtain the expected output.
I am trying to write a jolt transformation for the below input. but It's not matched with the expected output.
My Input:
[
{
"external_id": 855644012615,
"order_id": 4551472709703,
"processed_at": "2022-08-30T02:40:26-04:00",
"tracking_number": 4.201110410179302e 24,
"refund_line_items": [
{
"order_line_item_id": 11544859803719,
"quantity": 6,
"line_item": {
"name": "FLORENCIA RED MULTI - RED MULTI / 6 / 634",
"product_id": 6586562773063,
"properties": [
{
"name": "UPCA",
"value": "195945037714"
}
]
}
}
]
}
]
expected output
[
{
"externalId" : 855644012615,
"orderExternalId" : 4551472709703,
"partyIdFrom" : "NA",
"destinationFacilityId" : "",
"partyIdTo" : "NA",
"status" : "PURCH_SHIP_CREATED",
"type" : "SALES_RETURN",
"estimatedShipCost" : "0",
"packages" : [
{
"packageSeqId" : "",
"trackingCode" : 4.201110410179302E24,
"items" : [
{
"sku" : "195945037714",
"quantity" : 6
}
]
}
]
}
]
I am using this spec for transforming the input JSON. Can you please help to find out my mistake?
[
{
"operation": "default",
"spec": {
"*": {
"partyIdFrom": "_NA_",
"destinationFacilityId": "",
"externalPartyIdFrom": "",
"partyIdTo": "_NA_",
"status": "PURCH_SHIP_CREATED",
"type": "SALES_RETURN",
"estimatedShipCost": "0",
"packageSeqId": ""
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"external_id": "[&1].externalId",
"order_id": "[&1].orderExternalId",
"partyIdFrom": "[&1].partyIdFrom",
"destinationFacilityId": "[&1].destinationFacilityId",
"partyIdTo": "[&1].partyIdTo",
"status": "[&1].status",
"type": "[&1].type",
"estimatedShipCost": "[&1].estimatedShipCost",
"packageSeqId": "[&1].packages[&1].packageSeqId",
"tracking_number": "[&1].packages[&1].trackingCode",
"refund_line_items": {
"*": {
"quantity": "[&2].packages[#5].items[&3].quantity",
"line_item": {
"properties": {
"*": {
"value": "[&6].packages[#4].items[&1].sku"
}
}
}
}
}
}
}
}
]
CodePudding user response:
So far so good, just need to add the line
"@(3,quantity)": "[&6].packages[#4].items[&1].quantity"
into the "properties"
object, and replace the leaf nodes of some attributes with &
characters in order to replicate their key names. eg, no need to write them repeatedly. Those key-value pairs are :
"partyIdFrom": "[&1].&",
"destinationFacilityId": "[&1].&",
"partyIdTo": "[&1].&",
"status": "[&1].&",
"type": "[&1].&",
"estimatedShipCost": "[&1].&",
"packageSeqId": "[&1].packages[&1].&",
indeed using just one line
"*": "[&1].&",
is enough but the order would be mixed up for this case.
As a result, prefer using the following transformation spec :
[
{
"operation": "default",
"spec": {
"*": {
"partyIdFrom": "_NA_",
"destinationFacilityId": "",
"externalPartyIdFrom": "",
"partyIdTo": "_NA_",
"status": "PURCH_SHIP_CREATED",
"type": "SALES_RETURN",
"estimatedShipCost": "0",
"packageSeqId": ""
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"external_id": "[&1].externalId",
"order_id": "[&1].orderExternalId",
"partyIdFrom": "[&1].&", //should be converted to this
"destinationFacilityId": "[&1].&", //should be converted to this
"partyIdTo": "[&1].&", //should be converted to this
"status": "[&1].&", //should be converted to this
"type": "[&1].&", //should be converted to this
"estimatedShipCost": "[&1].&", //should be converted to this
"packageSeqId": "[&1].packages[&1].&", //should be converted to this
"tracking_number": "[&1].packages[&1].trackingCode",
"refund_line_items": {
"*": {
"quantity": "[&2].packages[#5].items[&3].&",
"line_item": {
"properties": {
"*": {
"value": "[&6].packages[#4].items[&1].sku",
"@(3,quantity)": "[&6].packages[#4].items[&1].quantity" //should be added
}
}
}
}
}
}
}
}
]
Btw, another option would be using #
wildcards as in the following example line
"#PURCH_SHIP_CREATED": "[&1].status",
The default transformation spec wouldn't be needed through use of this manner.
CodePudding user response:
The final answer of my input JSON that I expected.
[
{
"operation": "default",
"spec": {
"*": {
"partyIdFrom": "_NA_",
"destinationFacilityId": "",
"externalPartyIdFrom": "",
"partyIdTo": "_NA_",
"status": "PURCH_SHIP_CREATED",
"type": "SALES_RETURN",
"estimatedShipCost": "0",
"packageSeqId": ""
}
}
}, {
"operation": "shift",
"spec": {
"*": {
"external_id": "[&1].externalId",
"order_id": "[&1].orderExternalId",
"partyIdFrom": "[&1].partyIdFrom",
"destinationFacilityId": "[&1].destinationFacilityId",
"partyIdTo": "[&1].partyIdTo",
"status": "[&1].status",
"type": "[&1].type",
"estimatedShipCost": "[&1].estimatedShipCost",
"packageSeqId": "[&1].packages[#3].packageSeqId",
"tracking_number": "[&1].packages[#3].trackingCode",
"refund_line_items": {
"*": {
"quantity": "[&3].packages[#5].items[#2].quantity",
"line_item": {
"properties": {
"*": {
"value": "[&6].packages[#8].items[#5].sku"
}
}
}
}
}
}
}
}
]