Skip to content

Extract/Merge key and value trees from a JSON.

Notifications You must be signed in to change notification settings

arminrosu/json-lean

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON-Lean

Build Status Dependencies

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).

Installation

npm install json-lean

Example

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.'
});

Output - Keys

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"
]

Output - Values

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."
]

Recombine

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."
}

Even less?

Check out json-slim for a minifier better than JSON.stringify().

About

Extract/Merge key and value trees from a JSON.

Resources

Stars

Watchers

Forks

Packages

No packages published