All Projects → benjamin-allion → Json Node Normalizer

benjamin-allion / Json Node Normalizer

Licence: mit
'json-node-normalizer' - NodeJS module that normalize json data types from json schema specifications.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Json Node Normalizer

Univalue
High performance RAII C++ JSON library and universal value object class
Stars: ✭ 46 (-56.19%)
Mutual labels:  json-schema, json, json-data
Jsonschema Key Compression
Compress json-data based on its json-schema while still having valid json
Stars: ✭ 59 (-43.81%)
Mutual labels:  json-schema, json
Oakdex Pokedex
Ruby Gem and Node Package for comprehensive Generation 1-7 Pokedex data, including 809 Pokémon, uses JSON schemas to verify the data
Stars: ✭ 44 (-58.1%)
Mutual labels:  json-schema, json
I18n Generator
i18n json files generator for node, web browser and command line
Stars: ✭ 60 (-42.86%)
Mutual labels:  json, converter
Brutusin Rpc
Self-describing JSON-RPC web services over HTTP, with automatic API description based on JSON-Schema
Stars: ✭ 36 (-65.71%)
Mutual labels:  json-schema, json
Chinese Xinhua
📙 中华新华字典数据库。包括歇后语,成语,词语,汉字。
Stars: ✭ 8,705 (+8190.48%)
Mutual labels:  json, json-data
Lazyjson
A very fast, very lazy JSON parser for Java.
Stars: ✭ 55 (-47.62%)
Mutual labels:  json, json-data
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (-36.19%)
Mutual labels:  json, json-data
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (-36.19%)
Mutual labels:  json-schema, json
Json Ditto
Declarative JSON-to-JSON mapper .. shape-shift JSON files with Ditto 👻
Stars: ✭ 66 (-37.14%)
Mutual labels:  json, converter
Jsontocodable
A generating tool from Raw JSON to Codable (Swift4) text written in Swift4.
Stars: ✭ 33 (-68.57%)
Mutual labels:  json, converter
Avocado
Strongly-typed MongoDB driver for Rust
Stars: ✭ 70 (-33.33%)
Mutual labels:  json-schema, json
Jsonpath Rs
JSONPath for Rust
Stars: ✭ 31 (-70.48%)
Mutual labels:  json, json-data
Uvicorn Gunicorn Fastapi Docker
Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.6 and above with performance auto-tuning. Optionally with Alpine Linux.
Stars: ✭ 1,014 (+865.71%)
Mutual labels:  json-schema, json
Fastapi
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Stars: ✭ 39,588 (+37602.86%)
Mutual labels:  json-schema, json
Spectral
A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI v2 & v3.
Stars: ✭ 876 (+734.29%)
Mutual labels:  json-schema, json
Jsonlite
A simple, self-contained, serverless, zero-configuration, json document store.
Stars: ✭ 819 (+680%)
Mutual labels:  json, json-data
Movement
Movement is an easier, simpler way to explore and use NIEM. Want to join the Movement and contribute to it? Start here.
Stars: ✭ 19 (-81.9%)
Mutual labels:  json-schema, json
Inspec tools
A command-line and ruby API of utilities, converters and tools for creating, converting and processing security baseline formats, results and data
Stars: ✭ 65 (-38.1%)
Mutual labels:  json, converter
Jquery Jsontotable
This plugin can convert JSON data type to table for html. JSON is used primarily to transmit data between a server and web application, as an alternative to XML. In these reasons todays many applications use json data format for data transferring. And you need json to table converter for html display.
Stars: ✭ 69 (-34.29%)
Mutual labels:  json, json-data

Json-Node-Normalizer

NodeJS module that normalize json data types from json schema specifications.

npm version CircleCI Codacy Badge Codacy Badge JavaScript Style Guide Node 12.3 Uses yarn License

Json-Node-Normalizer Schema

Features

  • Convert / Cast Json Node type to another type :
    • From Json Schema Specifications
    • From Json Path
  • Supported types :
    • string
    • number, integer
    • array
    • boolean
    • null
  • Json Schema $Ref / Definitions support

Installation

Add the latest version of json-node-normalizer to your package.json:

npm install json-node-normalizer --save

Node.js Usage

const JsonNodeNormalizer = require('json-node-normalizer');
const normalizedJson = await JsonNodeNormalizer.normalize(jsonData, jsonSchema);

Use case

We have a json object with incorrect type formats :

const jsonData = {
   "fields":{
      "id": 123, // Must be a string
      "name":"my_name",
      "firstName":"firstName",
      "age": "31", // Must be a number
      "phone": "33600000010", // Must be a number
      "orders":{ // Must be an array
         "label": "first_order"
      },
      "externalData": {
         "id": "1234"
      },  // Must be a null
      "active": "true" // Must be a boolean
   }
}

We want to normalize json object to match with a Json Schema :

const jsonSchema = {
   "fields":{
      "type":"object",
      "properties":{
         "id":{
            "type": "string"
         },
         "name":{
            "type": "string"
         },
         "firstName":{
            "type": "string"
         },
         "age":{
            "type": "number"
         },
         "phone":{
            "type": "integer"
         },
         "orders":{
            "type": "array",
            "items":{
               "label":{
                  "type": "string"
               }
            }
         },
         "externalData": {
            "type": "null"
         },
         "active":{
            "type": "boolean"
         }
      }
   }
}

We can use JsonNodeNormalizer to normalize our json data :

const JsonNodeNormalizer = require('json-node-normalizer');
const result = await JsonNodeNormalizer.normalize(jsonData, jsonSchema);

Result :

result = {
   "fields":{
      "id": "123",
      "name": "my_name",
      "firstName": "firstName",
      "age": 31,
      "phone": 33600000010,
      "orders":[{
         "label": "first_order"
      }],
      "externalData": null,
      "active": true
   }
}

Other Example

Code sample :

    // Given
    const dataToNormalize = { 
      data: { 
        enable: 'true' // MUST BE CONVERTED TO BOOLEAN
      } 
    };
    const jsonSchema = {
      data: {
        type: 'object',
        properties: {
          enable: {
            type: 'boolean'
          }
        }
      }
    };
    const result = await JsonNodeNormalizer.normalize(dataToNormalize, jsonSchema);

Result :

result = {
   "data":{
      "enable": true
   }
}

You can find some other examples in 'tests' project folder.

Normalize node(s) from path (Without Json-Schema)

You can also use normalizePath method if you do not want to use the schema json.

const { JsonNodeNormalizer, NodeTypes } = require('json-node-normalizer');
let normalizedJson = JsonNodeNormalizer.normalizePath(jsonData, '.fields.id', NodeTypes.NUMBER_TYPE);
normalizedJson = JsonNodeNormalizer.normalizePath(jsonData, '.fields.orders', NodeTypes.ARRAY_TYPE);
normalizedJson = JsonNodeNormalizer.normalizePath(jsonData, '.fields.orders[*].label', NodeTypes.STRING_TYPE);

// You can also normalize each element with name 'active' for example...
normalizedJson = JsonNodeNormalizer.normalizePath(jsonData, '..active', NodeTypes.BOOLEAN_TYPE);

Play with Swagger 2 & Openapi 3 specification

In Swagger 2 and Openapi 3 specification, you can use $ref, allOf, anyOf, oneOf in definition of objects

If you want use a definition of object with this key words, you need flatter the definition like this:

const openapi_spec_flattered = JsonNodeNormalizer.oasFlatten(openapi_spec);

Example with a Swagger 2 specification:

cont openapi_spec = require('./docs/my-swagger.json');
openapi_spec_flattered = JsonNodeNormalizer.oasFlatten(openapi_spec);
...
jsonData = {
   id: 1
   name: 'Rex',
   color: 'brown chocolate'
}
...
const normalizedJson = await JsonNodeNormalizer.normalize(jsonData, openapi_spec_flattered.definitions.Pet);
...

JsonPath Documentation

See https://github.com/json-path/JsonPath for more information about JsonPath expressions.

Logging Level

Log events can have different severity levels - in some cases, you just want to log events with at least a warning level, sometimes log lines have to be more verbose.

Each level is given a specific integer priority. The higher the priority the more important the message is considered to be.

Level Priority
debug 4
info (default) 2
error 0

By default the logging level is set to 'info'.

You can override the logging level by setting the JSON_NODE_NORMALIZER_LOGGING_LEVEL environment variable.

JsonNodeNormalizer Configuration

For more specific usages, you can specify some configuration parameters when you use 'normalize' method :

Normalization type field name

Could be used in case that you want to use other field than 'type' to specify the target normalization type.

Code sample :

    // Given
    const dataToNormalize = { 
      data: { 
        enable: 'true' // MUST BE CONVERTED TO BOOLEAN
      } 
    };
    const jsonSchema = {
      data: {
        type: 'object',
        properties: {
          enable: {
            normalization_type: 'boolean'  // 'type' by default but in that case we want to use 'normalization_type'
          }
        }
      }
    };
    const config = {
      fieldNames: {
        type: 'normalization_type' // Configure target normalization field name here !
      }
    };
    const result = await JsonNodeNormalizer.normalize(dataToNormalize, jsonSchema, config);

Result :

result = {
   "data":{
      "enable": true
   }
}

License

MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].