Document 4 — Introducing Children Recursive Nesting
So far, every node has been simple. Now we make the structure slightly more interesting.
We will do two things:
- Add a child to an existing root node.
- Add a new root node and give that new node a child.
The traversal engine itself still does not change.
1. Add a Child to an Existing Node
Start with the existing customer node:
{
"customer": {
"name": "Keshav"
},
"order": {
"id": "ORD-101"
}
}
Now give customer another child: email.
{
"customer": {
"name": "Keshav",
"email": "keshav@example.com"
},
"order": {
"id": "ORD-101"
}
}
2. Add a New Root with Its Own Child
Now add product at the root, and give it a child called code:
{
"customer": {
"name": "Keshav",
"email": "keshav@example.com"
},
"order": {
"id": "ORD-101"
},
"product": {
"code": "P-100"
}
}
3. Describe the New Structure with Instructions
{
"customer": {
"name": {
"transform": "logPath"
},
"email": {
"transform": "logPath"
}
},
"order": {
"id": {
"transform": "logPath"
}
},
"product": {
"code": {
"transform": "logPath"
}
}
}
Notice what happened. We did not add another traversal algorithm for children. The same traversal simply continues deeper into the source.
product → code
4. The Function Still Does Not Change
function logPath(value, path) {
console.log("I am here in the tree:", path, value);
}
The function knows nothing about customer,
email, product, or code.
Those values come from the source at runtime.
5. The Traversal Reaches the New Locations
I am here in the tree: customer.name Keshav
I am here in the tree: customer.email keshav@example.com
I am here in the tree: order.id ORD-101
I am here in the tree: product.code P-100
The Point
The structure has become deeper, but the traversal has not become more complicated.