简体   繁体   English

JOLT 转换:根据嵌套元素中的值更改字段值

[英]JOLT Transformation: Change field value based on the value in a nested element

I have a service that receives a JSON and needs to transform it to a different structure before sending to a third party API. Our team is using JOLT for transformations in the application.我有一个接收 JSON 的服务,需要在发送给第三方 API 之前将其转换为不同的结构。我们的团队在应用程序中使用JOLT进行转换。 I have detailed the current scenario below along with the new ask.我在下面详细说明了当前情况以及新问题。 The issue with the new ask is that the elements/fields in the resultant JSON have to be derived based on the value of a nested element.新问题的问题是结果 JSON 中的元素/字段必须基于嵌套元素的值派生。 I have spent several hours trying different operations (mainly shift and modify-overwrite-beta) but have not been able to find a solution.我花了几个小时尝试不同的操作(主要是 shift 和 modify-overwrite-beta),但未能找到解决方案。

Source JSON来源 JSON

{
  "id": "wert23sd-0891-4fcd-ae31-380c0ef61198",
  "topic": "cartsaleOmni/sale",
  "subject": "EventTypeEvent.Cart.Sale",
  "data": {
    "payload": {
      "content": {
        "cartEvent": {
          "eventOccurrenceTime": "2023-01-12T03:09:42.254Z",
          "cartEventId": "fe9c22ca-dc38-4bcd-a220-c7425b9bed7e",
          "eventTriggerTime": "2023-01-12T02:38:43.609Z",
          "eventName": "Sale Cart",
          "cart": {
            "cartId": "be8b22ba-dc38-4bcd-a120-c7425b9bed7e",
            "cartLineItems": [
              {
                "itemId": "44e610ab-c209-4232-8bb4-51f7b9b13a75",
                "cartLineItemId": "fe9c22ca-23ad-46e0-8629-a6593597f183",
                "startsAt": "2023-01-16",
                "endsAt": "2023-01-19",
                "numberOfUnits": 2,
                "clientChannel": "web",
                "clientSource": "mgmri",
                "itemSelectionDetails": {
                  "extSelectionDetails": {
                    "isP1Customer": true
                  }
                },
                "eventType": "saleEvent.44e610ab-c209-4232-8bb4-51f7b9b13a75"
              }
            ]
          }
        }
      }
    }
  },
  "customerId": "CORP11A38249"
}

Current JOLT Spec当前 JOLT 规范

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "payload": {
          "content": {
            "cartEvent": {
              "cart": {
                "cartLineItems": {
                  "*": {
                    "eventType": "=concat('carteSaleEvent.', @(1,itemId))"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "customerId": "requests[0].payload.context.CustomerID",
      "data": {
        "payload": {
          "content": {
            "cartEvent": {
              "cart": {
                "cartId": "referenceNumber",
                "cartLineItems": {
                  "*": {
                    "eventType": "requests[0].eventType",
                    "$": "retryConfigRequestMap.@eventType.maxAttempts",
                    "itemId": "requests[0].payload.context.itemId",
                    "numberOfUnits": "requests[0].payload.context.noOfUnits",
                    "startsAt": "requests[0].payload.context.earliestDate",
                    "endsAt": "requests[0].payload.context.latestDate"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "retryConfigRequestMap": {
        "*": {
          "altEventName": "cartSaleComplete",
          "enabled": true
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "eventType": "Cart.Sale.Complete"
    }
  }
]

Current State Output JSON当前 State Output JSON

{
  "eventType": "Cart.Sale.Complete",
  "referenceNumber": "be8b22ba-dc38-4bcd-a120-c7425b9bed7e",
  "requests": [
    {
      "eventType": "carteSaleEvent.44e610ab-c209-4232-8bb4-51f7b9b13a75",
      "payload": {
        "context": {
          "CustomerID": "CORP11A38249",
          "earliestDate": "2023-01-16",
          "itemId": "44e610ab-c209-4232-8bb4-51f7b9b13a75",
          "latestDate": "2023-01-19",
          "noOfUnits": 2
        }
      }
    }
  ],
  "retryConfigRequestMap": {
    "carteSaleEvent.44e610ab-c209-4232-8bb4-51f7b9b13a75": {
      "altEventName": "cartSaleComplete",
      "enabled": true,
      "maxAttempts": "0"
    }
  }
}

Now I am required to change eventType to cartSaleEvent.PREMIUM in elements in requests array and also in the retryConfigRequestMap if the value of ...itemSelectionDetails.extSelectionDetails.isP1Customer is true .现在,如果...itemSelectionDetails.extSelectionDetails.isP1Customer retryConfigRequestMap值为true ,我需要将requests数组中的元素以及cartSaleEvent.PREMIUM中的eventType更改为 cartSaleEvent.PREMIUM 。 and remain the same if that field is set to 'false' or is not present.如果该字段设置为“false”或不存在,则保持不变。 So the desired JSON should look like below:所以所需的 JSON 应该如下所示:

{
  "eventType": "Cart.Sale.Complete",
  "referenceNumber": "be8b22ba-dc38-4bcd-a120-c7425b9bed7e",
  "requests": [
    {
      "eventType": "carteSaleEvent.PREMIUM",
      "payload": {
        "context": {
          "CustomerID": "CORP11A38249",
          "earliestDate": "2023-01-16",
          "itemId": "44e610ab-c209-4232-8bb4-51f7b9b13a75",
          "latestDate": "2023-01-19",
          "noOfUnits": 2
        }
      }
    }
  ],
  "retryConfigRequestMap": {
    "carteSaleEvent.PREMIUM": {
      "altEventName": "cartSaleComplete",
      "enabled": true,
      "maxAttempts": "0"
    }
  }
}

Apologies for the lengthy question.很抱歉这个冗长的问题。 I read through the documentation and also the below links but could not device a working solution.我通读了文档和以下链接,但无法找到可行的解决方案。 Any help/pointers will be greatly appreciated.任何帮助/指针将不胜感激。

You can rearrange the last shift transformation spec by using a conditional logic through matching "@(0,itemSelectionDetails.extSelectionDetails.isP1Customer)" identifier with true such as您可以通过将"@(0,itemSelectionDetails.extSelectionDetails.isP1Customer)"标识符与true匹配,使用条件逻辑重新排列最后一个班次转换规范,例如

{
  "operation": "shift",
  "spec": {
    "customerId": "requests[0].payload.context.CustomerID",
    "data": {
      "payload": {
        "content": {
          "cartEvent": {
            "cart": {
              "cartId": "referenceNumber",
              "cartLineItems": {
                "*": {
                  "@(0,itemSelectionDetails.extSelectionDetails.isP1Customer)": {
                    "true": {
                      "#carteSaleEvent\\.PREMIUM": "requests[0].eventType",
                      "$2": "retryConfigRequestMap.carteSaleEvent\\.PREMIUM.maxAttempts"
                    },
                    "*": {
                      "@(2,eventType)": "requests[0].eventType",
                      "$2": "retryConfigRequestMap.@(3,eventType).maxAttempts"
                    }
                  },
                  "itemId": "requests[0].payload.context.itemId",
                  "numberOfUnits": "requests[0].payload.context.noOfUnits",
                  "startsAt": "requests[0].payload.context.earliestDate",
                  "endsAt": "requests[0].payload.context.latestDate"
                }
              }
            }
          }
        }
      }
    }
  }
}

where we've gone two levels deeper by using $2 , "@(2,eventType)" or "@(3,eventType)" ( which needs to traverse one more level as stated on the Right-hand-side )我们通过使用$2"@(2,eventType)""@(3,eventType)"深入两层(如右手边所述,需要再遍历一层

Btw, if the order of the attributes matter, then you can add a sort transformation spec to the end just before closing square bracket ] such as顺便说一句,如果属性的顺序很重要,那么您可以在结束方括号]之前添加一个排序转换规范,例如

  ,
  {
    "operation": "sort"
  }

you can use this spec:你可以使用这个规范:

I moved all of your operations to one shift jolt spec, and create a temp variable with this value:我将您的所有操作移至一个shift jolt 规范,并使用此值创建一个temp变量:

If isP1Customer is true : carteSaleEvent.PREMIUM .如果isP1CustomertruecarteSaleEvent.PREMIUM

If isP1Customer is false : eventType value.如果isP1CustomerfalseeventType值。

And finally, you can use the temp value.最后,您可以使用temp值。

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "eventType": "=concat('carteSaleEvent.', @(1,itemId))"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "customerId": "requests[0].payload.context.CustomerID",
      "#Cart.Sale.Complete": "eventType",
      "data": {
        "*": {
          "*": {
            "*": {
              "cart": {
                "cartId": "referenceNumber",
                "cartLineItems": {
                  "*": {
                    "eventType": "temp",
                    "#test": "requests[&1].test",
                    "$": "retryConfigRequestMap.@eventType.maxAttempts",
                    "#cartSaleComplete": "retryConfigRequestMap.@eventType.altEventName",
                    "#true": "retryConfigRequestMap.@eventType.enabled",
                    "itemId": "requests[&1].payload.context.&",
                    "numberOfUnits": "requests[&1].payload.context.noOfUnits",
                    "startsAt": "requests[&1].payload.context.earliestDate",
                    "endsAt": "requests[&1].payload.context.latestDate",
                    "itemSelectionDetails": {
                      "*": {
                        "*": {
                          "true": {
                            "#carteSaleEvent.PREMIUM": "temp"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "temp": "=lastElement"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "eventType": "&",
      "requests": {
        "*": {
          "*": "&2[&1].&",
          "test": {
            "@(4,temp)": "&3[&2].eventType"
          }
        }
      },
      "retryConfigRequestMap": {
        "*": "&1.@(2,temp)"
      }
    }
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM