Transform a JSON object into a particular format using a specific schema.
json-structure-schema is small library that transform a given JSON object in another which respects a given JSON schema.
This library can be of interest if you retrieve external sources and need to be reformatted. In this case, you can make iterations and conditions to set up a structure that meets your needs. This library responds to this need by providing:
- a simple function that requires a simple JSON object and a schema
- and an executable that also offers the same functionality as the function.
This library was developed with the programming language: TypeScript.
To install to using npm package manager or Yarn:
$ npm install json-structure-schema
$ yarn add json-structure-schema
After installation, You can use the library this way:
import { transform } from "json-structure-schema";
const value = { key: "package.name" };
const schema = {
name: { __pattern__: "key" }
}
const transformed = transform(value, schema); // { name: "package.name" }
If you want to have a quick overview of all the features of this library, take a look at the example or integrations tests.
You have possibility to run in command-line this library without create a small JavaScript file. This executable provides all the options that are available at the code level. To see all the options, run the following line:
json-structure-schema --help
Property names are indeed reserved in the case of this library:
-
__pattern__:
Defines the path to source key. This property is mandatory: if it is not present in the JSON object of the schema, the library considered it a simple object. Example:
{ "__pattern__": "node1.node2.key3"}
__required__
Defines if the source key is required or not. This property can be used with __default__
: if the value does not exist, the default property is used to set an alternative value. Example:
{ "__required__": "true"}
__default__
Defines the default value if the source key is missing. In the case of an existing value, this property is ignored. Example:
{ "__default__": "Default value"}
__converter__
Defines a converter used to convert the value of the source key to another type or value. Example:
{
__default__: function (value) {
return "Value: " + value;
}
}
This project is licensed under the MIT License - see the LICENSE file for details