jsonschema-key-compression
Compress json-data based on its json-schema while still having valid json.
It works by compressing long attribute-names into smaller ones and backwards.
For example this:
{
"firstName": "Corrine",
"lastName": "Ziemann",
"title": "Ms.",
"gender": "f",
"zipCode": 75963,
"countryCode": "en",
"birthYear": 1960,
"active": false,
"shoppingCartItems": [
{
"productNumber": 29857,
"amount": 1
},
{
"productNumber": 53409,
"amount": 6
}
]
}
becomes this:
{
"|e": "Corrine",
"|g": "Ziemann",
"|j": "Ms.",
"|f": "f",
"|k": 75963,
"|d": "en",
"|c": 1960,
"|a": false,
"|i": [
{
"|h": 29857,
"|b": 1
},
{
"|h": 53409,
"|b": 6
}
]
}
Usage
Install
npm install jsonschema-key-compression --save
createCompressionTable
Creates a compression-table from the json-schema.
import {
createCompressionTable
} from 'jsonschema-key-compression';
const compressionTable = createCompressionTable(jsonSchema);
compressObject
Compress a json-object based on its schema.
import {
compressObject
} from 'jsonschema-key-compression';
const compressedObject = compressObject(
compressionTable,
jsonObject
);
decompressObject
Decompress a compressed object.
import {
decompressObject
} from 'jsonschema-key-compression';
const jsonObject = decompressObject(
compressionTable,
compressedObject
);
compressedPath
Transform a chain of json-attributes into its compressed format.
import {
compressedPath
} from 'jsonschema-key-compression';
const compressed = compressedPath(
compressionTable,
'whateverNested.firstName'
);
decompressedPath
Decompress a compressed path.
import {
decompressedPath
} from 'jsonschema-key-compression';
const decompressed = decompressedPath(
compressionTable,
'|a.|b'
);
compressQuery
Compress a mango-query so that it can run over a NoSQL-database that has stored compressed documents.
import {
compressQuery
} from 'jsonschema-key-compression';
const compressed = compressQuery(
compressionTable,
{
selector: {
active: {
$eq: true
}
},
skip: 1,
limit: 1,
fields: [
'id',
'name'
],
sort: [
'name'
]
}
);