A JSON to JSON mapper in TypeScript
npm install object-unpackerThis program takes a JSON object and convertes it to a new JSON object using a mapping specification, also written as JSON.
There are some examples of how to use the mapper in the test directory, and a simple TypeScript example follows:
``TypeScript
// Import the Schema Mapper type
import { createObjectUnpacker } from '../src/ObjectUnpacker';
import { ProcedureInstruction } from '../src/instructions';
// Create the schema mapper
const mapper = createObjectUnpacker();
// This is the data to be transformed
const data = {
"x": [
{
"a": [
"testa",
"testb"
],
"au": "%system.metadata.author"
}
]
};
// This is the transformation specification
const mapperData: ProcedureInstruction = [
{
comment: 'Create the top level result object',
action: 'toObject',
source: '/compact',
target: '/expanded',
keys: ['x'],
values: {
formatted: '%x'
}
},
{
comment: 'process the formatted array',
action: 'foreach',
source: '/expanded.formatted',
target: '/expanded.formatted',
loopVar: 'i',
instruction: [
{
comment: 'process the array entry',
action: 'toObject',
source: 'i',
target: 'tmp',
keys: ['a', 'au'],
values: {
another:{
first:'%a.0',
second:'%a.1',
fourth:'%/subs.system.metadata.name'
},
author:'%au'
}
}
]
}
];
// Entries in this object can be referenced by the data or the mapperData.%system.metadata.name
// E.g. and '%system.metadata.author'
const refs = {
system: {
metadata: {
name: 'The System Name',
author: 'A. Programmer',
},
},
};
// Apply the mapping to the data to get an expanded object
const expanded: object = mapper.convert(refs, data, mapperData);
// Pretty-print the resulting JSON.
console.log(JSON.stringify(expanded, null, 2));
`
JavaScript
const unpacker = require("object-unpacker")// This is the data to be transformed
const data = {
"x": [
{
"a": [
"testa",
"testb"
],
"au": "%system.metadata.author"
}
]
};
// This is the transformation specification
ProcedureInstruction = [
{
comment: 'Create the top level result object',
action: 'toObject',
source: '/compact',
target: '/expanded',
keys: ['x'],
values: {
formatted: '%x'
}
},
{
comment: 'process the
formatted array',
action: 'foreach',
source: '/expanded.formatted',
target: '/expanded.formatted',
loopVar: 'i',
instruction: [
{
comment: 'process the array entry',
action: 'toObject',
source: 'i',
target: 'tmp',
keys: ['a', 'au'],
values: {
another:{
first:'%a.0',
second:'%a.1',
fourth:'%/subs.system.metadata.name'
},
author:'%au'
}
}
]
}
];
// Entries in this object can be referenced by the data or the mapperData.
// E.g. %system.metadata.name and '%system.metadata.author'
const refs = {
system: {
metadata: {
name: 'The System Name',
author: 'A. Programmer',
},
},
};// Apply the mapping to the data to get an
expanded object
const expanded = unpacker.mapper.convert(refs, data, mapperData);// Pretty-print the resulting JSON.
console.log(JSON.stringify(expanded, null, 2));
`Running the example code
1. Run
npm install
2. Create scratch.ts in the src directory.
3. Compile it using the tsc command
4. Run it using node dist/scratch.js
5. Or combine the previous two steps as tsc && node dist/scratch.jsThe output should look like this:
`json
{{
"formatted": [
{
"another": {
"first": "testa",
"second": "testb",
"fourth": "The System Name"
},
"author": "A. Programmer"
}
]
}
`Running the example code in a web browser
1. Run
npm install
2. Run npm install -g webpack
3. Run npm install -g webpack-cli
4. Run the webpack command in the project root directory.
5. Open the test/index.html file using a web browser (Only tested using Brave browser)
6. Paste your JSON into the first two text areas and click Execute`