{"id":13397847,"url":"https://github.com/benjamin-allion/json-node-normalizer","last_synced_at":"2025-03-14T00:32:46.149Z","repository":{"id":34947931,"uuid":"190174563","full_name":"benjamin-allion/json-node-normalizer","owner":"benjamin-allion","description":"'json-node-normalizer' - NodeJS module that normalize json data types from json schema specifications.","archived":false,"fork":false,"pushed_at":"2024-09-12T06:37:05.000Z","size":745,"stargazers_count":112,"open_issues_count":5,"forks_count":10,"subscribers_count":4,"default_branch":"develop","last_synced_at":"2024-09-16T11:27:47.836Z","etag":null,"topics":["cast","converter","json","json-data","json-schema","nodejs","nodejs-modules","normalization","transformation"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benjamin-allion.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-04T09:52:56.000Z","updated_at":"2024-08-16T11:28:34.000Z","dependencies_parsed_at":"2024-10-25T14:52:04.052Z","dependency_job_id":"8da3f880-99cb-42a8-bac9-5d1423ca539b","html_url":"https://github.com/benjamin-allion/json-node-normalizer","commit_stats":{"total_commits":172,"total_committers":8,"mean_commits":21.5,"dds":"0.19186046511627908","last_synced_commit":"805a12d808caa30ed342033537c02a2f3abe86df"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamin-allion%2Fjson-node-normalizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamin-allion%2Fjson-node-normalizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamin-allion%2Fjson-node-normalizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamin-allion%2Fjson-node-normalizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benjamin-allion","download_url":"https://codeload.github.com/benjamin-allion/json-node-normalizer/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243505055,"owners_count":20301549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cast","converter","json","json-data","json-schema","nodejs","nodejs-modules","normalization","transformation"],"created_at":"2024-07-30T18:01:48.627Z","updated_at":"2025-03-14T00:32:46.139Z","avatar_url":"https://github.com/benjamin-allion.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Json-Node-Normalizer\nNodeJS module that normalize json data types from json schema specifications.\n\n[![npm version](https://img.shields.io/npm/v/json-node-normalizer.svg?style=flat-square)](https://www.npmjs.com/package/json-node-normalizer)\n[![CircleCI](https://circleci.com/gh/benjamin-allion/json-node-normalizer/tree/master.svg?style=shield)](https://circleci.com/gh/benjamin-allion/json-node-normalizer/tree/master)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/607fffb36855477dbbb9c8fbdc65d246)](https://app.codacy.com/app/benjamin-allion/json-node-normalizer?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=benjamin-allion/json-node-normalizer\u0026utm_campaign=Badge_Grade_Dashboard)\n[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/9038356c6a6a4bae868867d1f7454ca8)](https://www.codacy.com/app/benjamin-allion/json-node-normalizer?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=benjamin-allion/json-node-normalizer\u0026utm_campaign=Badge_Coverage)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n[![Node 22.6](https://img.shields.io/badge/node%20version-22.6-green.svg)](https://nodejs.org/en/download/)\n[![Uses yarn](https://img.shields.io/badge/deps-yarn-blue.svg)]()\n[![License](https://img.shields.io/github/license/bojand/json-schema-deref.svg?style=flat-square)](https://raw.githubusercontent.com/bojand/json-schema-deref/master/LICENSE)\n\n![Json-Node-Normalizer Schema](docs/normalizer-schema.png)\n\n## Features\n\n* Convert / Cast Json Node type to another type :\n    * From Json Schema Specifications\n    * From Json Path\n* Supported types :\n    * `string`\n    * `number`, `integer`\n    * `array`\n    * `boolean`\n    * `null`\n* Json Schema $Ref / Definitions support    \n\n## Installation\n\nAdd the latest version of `json-node-normalizer` to your package.json:\n\n```sh\nnpm install json-node-normalizer --save\n```\n\n## Node.js Usage\n\n```javascript\nconst JsonNodeNormalizer = require('json-node-normalizer');\nconst normalizedJson = await JsonNodeNormalizer.normalize(jsonData, jsonSchema);\n```\n\n## Use case\n    \nWe have a json object with incorrect type formats :\n\n```javascript\nconst jsonData = {\n   \"fields\":{\n      \"id\": 123, // Must be a string\n      \"name\":\"my_name\",\n      \"firstName\":\"firstName\",\n      \"age\": \"31\", // Must be a number\n      \"phone\": \"33600000010\", // Must be a number\n      \"orders\": [{ \n         // Must contain a \"label\" fields with default value\n         \"articles\": { // Must be an array\n           \"price\": \"15.4\"\n         }\n      }],\n      \"externalData\": {\n         \"id\": \"1234\"\n      },  // Must be a null\n      \"active\": \"true\" // Must be a boolean\n   }\n}\n```\n\nWe want to normalize json object to match with a Json Schema :\n```javascript\nconst jsonSchema = {\n   \"fields\":{\n      \"type\":\"object\",\n      \"properties\":{\n         \"id\":{\n            \"type\": \"string\"\n         },\n         \"name\":{\n            \"type\": \"string\"\n         },\n         \"firstName\":{\n            \"type\": \"string\"\n         },\n         \"age\":{\n            \"type\": \"number\"\n         },\n         \"phone\":{\n            \"type\": \"integer\"\n         },\n         \"orders\":{\n            \"type\": \"array\",\n            \"items\":{\n               \"label\":{\n                  \"type\": \"string\",\n                  \"default\": \"Empty order\"\n               },\n               \"articles\": {\n                  \"type\": \"array\",\n                  \"items\": {\n                    \"price\": { \"type\": \"string\" }\n                  }\n               }\n            }\n         },\n         \"externalData\": {\n            \"type\": \"null\"\n         },\n         \"active\":{\n            \"type\": \"boolean\"\n         }\n      }\n   }\n}\n```\nWe can use JsonNodeNormalizer to normalize our json data :\n\n```javascript\nconst JsonNodeNormalizer = require('json-node-normalizer');\nconst result = await JsonNodeNormalizer.normalize(jsonData, jsonSchema);\n```\nResult :\n```javascript\nresult = {\n   \"fields\":{\n      \"id\": \"123\",\n      \"name\": \"my_name\",\n      \"firstName\": \"firstName\",\n      \"age\": 31,\n      \"phone\": 33600000010,\n      \"orders\":[{\n         \"label\": \"Empty order\", \n         \"articles\": [{\n           \"price\": \"15.4\"\n         }]\n      }],\n      \"externalData\": null,\n      \"active\": true\n   }\n}\n```\n\n## Other Example\n\nCode sample :\n```javascript\n    // Given\n    const dataToNormalize = { \n      data: { \n        enable: 'true' // MUST BE CONVERTED TO BOOLEAN\n      } \n    };\n    const jsonSchema = {\n      data: {\n        type: 'object',\n        properties: {\n          enable: {\n            type: 'boolean'\n          }\n        }\n      }\n    };\n    const result = await JsonNodeNormalizer.normalize(dataToNormalize, jsonSchema);\n```\n\nResult :\n```javascript\nresult = {\n   \"data\":{\n      \"enable\": true\n   }\n}\n```\n\nYou can find some other examples in 'tests' project folder. \n\n## Normalize node(s) from path (Without Json-Schema)\n\nYou can also use `normalizePaths` method if you do not want to use the schema json.\n\n```javascript\nconst { JsonNodeNormalizer, NodeTypes } = require('json-node-normalizer');\nlet normalizedJson = JsonNodeNormalizer.normalizePaths({ jsonNode: jsonData, paths: ['.fields.id'], type: NodeTypes.NUMBER_TYPE });\nnormalizedJson = JsonNodeNormalizer.normalizePaths({ jsonNode: jsonData, paths: ['.fields.orders'], type: NodeTypes.ARRAY_TYPE });\nnormalizedJson = JsonNodeNormalizer.normalizePaths({ jsonNode: jsonData, paths: ['.fields.orders[*].label'], type: NodeTypes.STRING_TYPE });\n\n// You can also normalize each element with name 'active' for example...\nnormalizedJson = JsonNodeNormalizer.normalizePaths({ jsonNode: jsonData, paths: ['..active'], type: NodeTypes.BOOLEAN_TYPE });\n```\n\n## Set default node(s) value from path (Without Json-Schema)\n\nYou can also use `normalizePaths` method to set default value (if value doesn't exist).\n\n```javascript\nconst { JsonNodeNormalizer, NodeTypes } = require('json-node-normalizer');\nlet normalizedJson = JsonNodeNormalizer.normalizePaths({ jsonNode: jsonData, paths: ['.fields.orders[*].label'], type: NodeTypes.STRING_TYPE, defaultValue: 'Empty Order' });\n```\n\n## Play with Swagger 2 \u0026 Openapi 3 specification\n\nIn Swagger 2 and Openapi 3 specification, you can use ```$ref```, ```allOf```, ```anyOf```, ```oneOf``` in definition of objects\n\nIf you want use a definition of object with this key words, you need flatter the definition like this:\n\n```\nconst openapi_spec_flattered = JsonNodeNormalizer.oasFlatten(openapi_spec);\n```\n\nExample with a Swagger 2 specification:\n```\ncont openapi_spec = require('./docs/my-swagger.json');\nopenapi_spec_flattered = JsonNodeNormalizer.oasFlatten(openapi_spec);\n...\njsonData = {\n   id: 1\n   name: 'Rex',\n   color: 'brown chocolate'\n}\n...\nconst normalizedJson = await JsonNodeNormalizer.normalize(jsonData, openapi_spec_flattered.definitions.Pet);\n...\n```\n\n## JsonPath Documentation\n\nSee https://github.com/json-path/JsonPath for more information about JsonPath expressions.\n\n## Logging Level\n\nLogging is disabled by default (since 1.0.10).\nTo enable logging, you must define the `JSON_NODE_NORMALIZER_DEBUG` environment to `true`.\n\nLog 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.\n\nEach level is given a specific integer priority. The higher the priority the more important the message is considered to be.\n\n| Level \t| Priority \t|\n|-------\t|----------\t|\n| debug \t| 4        \t|\n| info (default) \t| 2        \t|\n| error \t| 0        \t|\n\nBy default the logging level is set to 'info'.\n\nYou can override the logging level by setting the `JSON_NODE_NORMALIZER_LOGGING_LEVEL` environment variable.\n\n## JsonNodeNormalizer Configuration\n\nFor more specific usages, you can specify some configuration parameters when you use 'normalize' method : \n\n#### Normalization type field name\n\nCould be used in case that you want to use other field than 'type' to specify the target normalization type.\n\nCode sample :\n```javascript\n    // Given\n    const dataToNormalize = { \n      data: { \n        enable: 'true' // MUST BE CONVERTED TO BOOLEAN\n      } \n    };\n    const jsonSchema = {\n      data: {\n        type: 'object',\n        properties: {\n          enable: {\n            normalization_type: 'boolean'  // 'type' by default but in that case we want to use 'normalization_type'\n          }\n        }\n      }\n    };\n    const config = {\n      fieldNames: {\n        type: 'normalization_type' // Configure target normalization field name here !\n      }\n    };\n    const result = await JsonNodeNormalizer.normalize(dataToNormalize, jsonSchema, config);\n```\n\nResult :\n```javascript\nresult = {\n   \"data\":{\n      \"enable\": true\n   }\n}\n```\n\n#### Exclude some fields\n\nIf you need to exclude some fields to be normalized, you can use the configuration variable `excludePaths`\n\nCode sample :\n\n```javascript\n// Given\nconst dataToNormalize = {\n  data: {\n    enable: 'true',\n    count: '72',\n    other: '12',\n    foo: 5414325,\n  },\n};\nconst jsonSchema = {\n  data: {\n    type: 'object',\n    properties: {\n      enable: {\n        type: 'boolean',\n      },\n      count: {\n        type: 'number',\n      },\n      other: {\n        type: 'number',\n      },\n      foo: {\n        type: 'string',\n        format: 'date-time',\n      },\n    },\n  },\n};\nconst config = {\n  excludePaths: [\n    {\n      path: '$.data.enable', // Exclude by field path\n    },\n    {\n      type: 'number', // Exclude by type\n    },\n    {\n      type: 'string', // Exclude by both type and format\n      format: 'date-time',\n    },\n  ],\n};\nconst result = await JsonNodeNormalizer.normalize(dataToNormalize, jsonSchema, config);\n```\n\nResult :\n\n```javascript\nresult = {\n  data: {\n    enable: 'true',\n    count: '72',\n    other: '12',\n    foo: 5414325,\n  },\n};\n```\n\n#### Cache to increase performance\n\nIf your schema doesn't change between calls, you can enable cache to reduce process time.\n\nConfiguration variables :\n```javascript\n{\n  useCache: true,\n  cacheId: \"schemaId\", // Schema identifier used to put/get schema from cache.\n  cacheDuration: 60000 // Cache duration in milliseconds\n}\n```\n\nCode sample :\n```javascript\n    // Given\n    const dataToNormalize = { \n      data: { \n        enable: 'true' // MUST BE CONVERTED TO BOOLEAN\n      } \n    };\n    const jsonSchema = {\n      schemaName: \"mySchema\",      \n      data: {\n        type: 'object',\n        properties: {\n          enable: {\n            normalization_type: 'boolean'  // 'type' by default but in that case we want to use 'normalization_type'\n          }\n        }\n      }\n    };\n    const config = {\n      fieldNames: {\n        useCache: true,\n        cacheId: \"mySampleSchema\",\n        cacheDuration: 60000 // 60 seconds\n      }\n    };\n    const result = await JsonNodeNormalizer.normalize(dataToNormalize, jsonSchema, config);\n```\n\nResult :\n```javascript\nresult = {\n   \"data\":{\n      \"enable\": true\n   }\n}\n```\n\nNote :\n\nYou can use ```JsonNodeNormalizer.clearCache()``` to manually reset the library cache. \n\n## License\n\n[MIT License](http://www.opensource.org/licenses/mit-license.php).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenjamin-allion%2Fjson-node-normalizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenjamin-allion%2Fjson-node-normalizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenjamin-allion%2Fjson-node-normalizer/lists"}