How to use if-then-else and loop construct in input transformer in EventBridge?

0

Is there a way to define if-then-else and looping constructs using JSONPath, while defining the configuring the "input path" for EventBridge Input Transformer?

E.g. For the following input

{
            "category": {
              "name": "Nike Shoes",
              "level": 2,
              "translation": null,
              "ancestors": {
                "edges": [
                  {
                    "node": {
                      "name": "Shoes",
                      "slug": "shoes",
                      "level": 0
                    }
                  },
                  {
                    "node": {
                      "name": "Running Shoes",
                      "slug": "running-shoes",
                      "level": 1
                    }
                  }
                ]
              }
            }
}

I need the output to be

{
    "categories.lvl0": "Shoes",
    "categories.lvl1": "Shoes > Running shoes",
    "categories.lvl2": "Shoes > Running shoes > Nike shoes",
}

The following is the python logic for the output I wish to achieve

if node["category"]["level"] != 0:
    category_hierarchy = list(map(lambda x: x["node"]["name"], node["category"]["ancestors"]["edges"]))
    category_hierarchy.append(new_item["categoryName"])
    for i in range(len(category_hierarchy)):
        new_item[f'categories.lvl{i}'] = " > ".join(category_hierarchy[0:i+1])

if the level of the main category ("Nike shoes" here) is not equal to 0, then I want to loop through its ancestors and define variables of the form categories.lvl(n) with the logic defined in column 2 below, to get the values defined in column 3

VariableLogicValue required
category.lvl0ancestor category with level 0Shoes
category.lvl1ancestor category with level 0 > ancestor category with level 1Shoes> Running shoes
category.lvl2ancestor category with level 0 > ancestor category with level 1 > main category (with level 0)Shoes> Running shoes > Nike shoes

I could frame the following JSONPath construct for now, which in plain English, represents:

"if the category level is 0, then proceed to output names of the ancestor categories"

JSONPath - $..edges[?(@.level!=0)]..name Output

[
  "Shoes",
  "Running shoes"
]

However, I am not sure how to proceed further.

  • Not sure but it does not seem JSONpath can do what you want to do easily. How about using Lambda with the Python snippet and send the result to EventBridge?

satvik
질문됨 2년 전137회 조회
답변 없음

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠