Code Monkey home page Code Monkey logo

json-node-normalizer's Introduction

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
  • 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"
      },
      "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"
               }
            }
         },
         "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"
      }],
      "active": true
   }
}

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);

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.

License

MIT License.

json-node-normalizer's People

Contributors

benjamin-allion avatar rayedb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.