JSON Handler instead of Code Node
Parsing Using Legacy Code Node (JavaScript) - Deprecated use case
In older versions of Journey Builder (JB Pro), you'd use a Code Node and write custom JavaScript like this:
Sample API JSON Payload (Example)
{
"user": {
"id": 12345,
"name": "Alice",
"email": "[email protected]",
"orders": [
{
"orderId": "ORD001",
"amount": 250,
"status": "delivered"
},
{
"orderId": "ORD002",
"amount": 450,
"status": "processing"
}
]
}
}
Sample Code used to parse the JSON earlier:
//Assume variable `input` contains the API response JSON string\
let response = JSON.parse(var_local.input);
//Extract user details\
let userId = response.user.id;
let userName = response.user.name;
let userEmail = response.user.email;
// Extract first order details\
let firstOrder = response.user.orders\[0];
let orderId = firstOrder.orderId;
let orderAmount = firstOrder.amount;
let orderStatus = firstOrder.status;
// Store extracted values in output variables to use later in the journey\
output.userId = userId;
output.userName = userName;
output.userEmail = userEmail;
output.orderId = orderId;
output.orderAmount = orderAmount;
output.orderStatus = orderStatus;
This requires:
- Parsing JSON manually
- Writing and debugging JavaScript code
- Mapping extracted values to output variables manually
Parsing Using New JSON Handler Node (No-Code)
With the new JSON Handler Node introduced in JB V2 (Upgraded Journey Builder), you can achieve this without code, via an intuitive UI that lets you define JSON paths for mapping.
How it works:
- Add JSON Handler Node after your API Node.
- Configure mappings visually by specifying JSON paths and the variable names to map values to.
- More on How to use JSON Handler : Link
- Example mappings you would configure:
Output Variable | JSON Path | Description |
---|---|---|
userId | $.user.id | User ID |
userName | $.user.name | User Name |
userEmail | $.user.email | User Email |
orderId | $.user.orders[0].orderId | First order ID |
orderAmount | $.user.orders[0].amount | First order amount |
orderStatus | $.user.orders[0].status | First order status |
The JSON Handler node automatically parses the JSON payload from the previous API call and assigns the values to the specified variables. No coding is required, and the UI guides you to enter correct JSON paths.
Aspect | Code Node (Legacy) | JSON Handler Node (New) |
---|---|---|
Requires Coding? | Yes, JavaScript needed | No, visual mapping with JSON path expressions |
JSON Parsing | Manual (JSON.parse ) | Automatic |
Variable Mapping | Manual assignment in code | Configured via UI |
Suitable for Non-Coders | No | Yes |
Complexity Handling | Complex nested JSON needs custom code logic | Handles nested JSON via JSON path |
Debugging & Maintenance | Harder, requires dev knowledge | Easier, no code to maintain |
Updated 23 days ago