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
asked 6 months ago76 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions