Extract/Merge key and value trees from a JSON.
It reduces the size of your requests, if the client already has the key tree. Especially useful for repeating requests, as you can send the data tree alone (on average 1/3 smaller than a complete JSON).
npm install json-lean
var lean = require('json-lean');
var encoded = lean.encode({
'array': [
1,
2
],
'integer': 1042,
'boolean': false,
'object': {
'name': 'Oscar'
},
'string': 'I love deadlines. I like the whooshing sound they make as they fly by.'
});
This should be stored with the client.
Once the first (unencoded) request is made, you should make requests to the encoded endpoint.
Keys are sorted.
console.log(encoded[0]) ===
[
"array",
"boolean",
"integer",
{
"object": [
"name"
]
},
"string"
]
This example is 34% smaller than the original JSON with keys.
console.log(encoded[1]) ===
[
[
1,
2
],
false,
1042,
[
"Oscar"
],
"I love deadlines. I like the whooshing sound they make as they fly by."
]
Decoding is transparent. You can add it as a step before your actual data parsing.
lean.decode(encoded) ===
{
"array": [
1,
2
],
"boolean": false,
"integer": 1042,
"object": {
"name": "Oscar"
},
"string": "I love deadlines. I like the whooshing sound they make as they fly by."
}
Check out json-slim for a minifier better than JSON.stringify().