json-cascade is a pure json template(ish) library for browser or node.js. It takes several json objects and combines them to produce a single output. Like CSS, it uses cascading and precedence to determine which fields get output.
jsonCascade(template [, diff1] [, diffN])
jsonCascade = require('json-cascade');
out = jsonCascade(template [, diff1] [, diffN]);
<script src="underscore.js"></script>
<script src="json-cascade.js"></script>
out = jsonCascade(template [, diff1] [, diffN]);
The template object defines the structure of the output json. Only fields that appear in the template will be part of the output.
Each diff (aka model) is then applied to the template with the last diff having highest precedence.
{
customerId : null,
firstname : "",
lastname : "",
age : -1
}
{
"firstname" : "John",
"age" : 32
}
{
"lastname" : "Smith",
"age" : 48
}
{
customerId : null,
firstname : "John",
lastname : "Smith",
age : 48
}
To output an array of objects, the template should contain an array with exactly one object (not a primitive). The equivalent array in the diff may then have multiple objects which will be applied in a loop. If the template array does not contain exactly one object, the array will be overridden as if it were a primitive.
{
customerId : null,
firstname : "",
lastname : "",
age : -1,
address : {
line1 : "",
line2 : "",
line3 : "",
country : ""
},
relatedCustomers : [],
products : [
{
productId : -1,
title : "",
price : -1
}
]
}
{
firstname : "John",
age : 32,
address : {
line1 : "Aras An Uachtairain",
},
relatedCustomers : [2000005],
products : [
{
productId : 1000005,
title : "The Mythical Man Month",
price : 50
},
{
productId : 1000006,
title : "Effective Java",
price : 40
}
]
}
{
lastname : "Smith",
age : 48,
address : {
line2 : "Phoenix Park",
country : "Ireland"
},
relatedCustomers : [2000008, 2000009],
products : [
{
},
{
title : "Clean Code",
price : 100
},
{
productId : 1000009
}
]
}
{
customerId : null,
firstname : "John",
lastname : "Smith",
age : 48,
address : {
line1 : "Aras An Uachtairain",
line2 : "Phoenix Park",
line3 : "",
country : "Ireland"
},
relatedCustomers : [2000008, 2000009],
products : [
{
productId : -1,
title : "",
price : -1
},
{
productId : 1000006,
title : "Clean Code",
price : 100
},
{
productId : 1000009,
title : "",
price : -1
}
]
}