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
Variable | Logic | Value required |
---|
category.lvl0 | ancestor category with level 0 | Shoes |
category.lvl1 | ancestor category with level 0 > ancestor category with level 1 | Shoes> Running shoes |
category.lvl2 | ancestor 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?