Package Exports
- json-light
- json-light/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-light) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Json Light
Json Light is a json compressor, generating a new simplified json file from a schema definition.
The schema can be written by us or generated by the library itself.
Json Light extracts the property tags from a json object and stores the values in an array.
Features
- Compress json generating a new valid json
- Allows to get schema from json data
- In the event that you do not want to pass the schema to the compress function, it will be generated from the data and stored within the resulting json, allowing it to be later decompressed without the need to pass the schema
Methods
.schema(data:any)
get schema from json data
- Params:
- data : json data
.compress(data:any, schema?:string)
get a compressed json
- Params:
- data : json data
- schema : schema (optional)
.decompress(data:any, schema?:string)
decompress a previously compressed json getting the original json
- Params:
- data : json data
- schema : schema (optional)
Quick start
import { JsonLight } from 'json-light'
const data = {
name: 'Spain',
region: 'Europe',
phoneCode: '34',
timezones: [
{ name: 'Madrid', offset: 1, pos: { lat: 40.4165, log: -3.70256 } },
{ name: 'Ceuta', offset: 1, pos: { lat: 35.8883, log: -5.3162 } },
{ name: 'Canary', offset: 0, pos: { lat: 28.1248, log: -15.43 } }
]
}Schema:
const schema = JsonLight.schema(data)
console.log(schema)Output:
{ name:string,
region:string,
phoneCode:string,
timezones:[
{name:string,
offset:integer,
pos:{lat:decimal,log:decimal}
}
]
}Compress:
const compressed = JsonLight.compress(data, schema)
console.log(JSON.stringify(compressed, null, 2))Output:
{
"_": [
"Spain",
"Europe",
"34"
],
"timezones": [
{
"_": [
"Madrid",
1
],
"pos": [
40.4165,
-3.70256
]
},
{
"_": [
"Ceuta",
1
],
"pos": [
35.8883,
-5.3162
]
},
{
"_": [
"Canary",
0
],
"pos": [
28.1248,
-15.43
]
}
]
}Decompress:
const decompressed = JsonLight.decompress(compressed, schema)
console.log(JSON.stringify(decompressed, null, 2))Output:
{
"name": "Spain",
"region": "Europe",
"phoneCode": "34",
"timezones": [
{
"name": "Madrid",
"offset": 1,
"pos": {
"lat": 40.4165,
"log": -3.70256
}
},
{
"name": "Ceuta",
"offset": 1,
"pos": {
"lat": 35.8883,
"log": -5.3162
}
},
{
"name": "Canary",
"offset": 0,
"pos": {
"lat": 28.1248,
"log": -15.43
}
}
]
}CLI
Install
npm install -g json-light Version:
json-light versionSchema:
json-light schema -i source.jsonCompress:
json-light compress -i source.json -o compressed.json -s '{name:string,region:string,phoneCode:string,timezones:[{name:string,offset:integer,pos:{lat:decimal,log:decimal}}]}' Decompress:
json-light decompress -i compressed.json -o original.json -s '{name:string,region:string,phoneCode:string,timezones:[{name:string,offset:integer,pos:{lat:decimal,log:decimal}}]}'