Document 2 — Two Root Nodes Minimal Example
The minimal working example: traversing a 2-node source (customer, order) and attaching behavior at runtime.
1. The Source Data
Consider a minimal source with two root nodes:
{
"customer": {
"name": "Keshav"
},
"order": {
"id": "ORD-101"
}
}
2. The Instructions
We want both nodes to invoke a function called logPath:
{
"customer": {
"transform": "logPath"
},
"order": {
"transform": "logPath"
}
}
3. The External Function
The function is declared outside the JSON, in pure JavaScript:
function logPath(value, path) {
console.log("I am here in the tree:", path, value);
}
Notice that logPath does not know the tree shape in advance.
It simply logs whatever path and value the traversal engine hands to it.
4. Traversal Execution
When the engine executes, it visits each source node, matches the instruction, and binds the function:
1. Visit customer → Find "transform": "logPath" → Bind logPath
2. Visit order → Find "transform": "logPath" → Bind logPath
5. Runtime Output
Invoking the resulting leaf functions prints:
I am here in the tree: customer.name Keshav
I am here in the tree: order.id ORD-101
6. The Key Takeaway
The engine walked the source, consulted the instructions, and bound the external function to every instructed leaf.
2 Source Nodes → 2 Instructions → 2 Function Calls → Zero hard-coded paths in the function