# compare-validate-json

> A node package to compare two JSON objects with levels deep till 16 levels.

Latest version **1.1.1** (published 2018-10-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install compare-validate-json
pnpm add compare-validate-json
yarn add compare-validate-json
bun add compare-validate-json
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2018-10-26 |
| First published | 2018-10-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ajay GT |
| Maintainers | ajaygopal07 |
| Keywords | json, compareJson, comparison, equal, subset, superset, validation, compareTwoJson, deepCompareJson, deepCompare, deep, strict, compare |

## Links

- npm: https://www.npmjs.com/package/compare-validate-json
- npm.io page: https://npm.io/package/compare-validate-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.1 (latest) — 2018-10-26
- 1.1.0 — 2018-10-26
- 1.0.5 — 2018-10-25
- 1.0.4 — 2018-10-25
- 1.0.3 — 2018-10-25
- 1.0.2 — 2018-10-25
- 1.0.1 — 2018-10-25
- 1.0.0 — 2018-10-25

## README

# compare-validate-json.js 

**compare-validate-json.js** is usefull in deep comparing first json with second. Specially for http response validation which are in JSON. 

## Installation

If you are using node, you can install compare-validate-json with [npm](https://npmjs.org/).

    npm install compare-validate-json

Then you can include it in your code:

	var comapareValidateJson = require("compare-validate-json");


## Usage
### `comapareValidateJson.compare(json1, json2)`
### `comapareValidateJson.deepStrictCompare(json1, json2)`

## Description
The first  json to be passed to the compare function can be a subset of the second json. The compare function skips the missing key not present in the first json and present in the second json. If a key present in the first json is not present in the second json then the valdation will fail.
This is useful when we want to validate for a json response,but we only want to provide the keys which we want to validate and skip the other keys. This also works for circular JSONs also.
For deep and strict json comparison we also have another api "deepStrictCompare", which will return true only if both the json passed to it are identical in all aspects.
## Examples:
    var comapareValidateJson = require("compare-validate-json");

    var json1 = {"a":"val1","arry":["a","b","c"],"obj":{"obj1":"val1"},"arrObj":[{"artObj1":"arrObjVal1"}],"ObjInsObj":{"ObjInsObj1":{"obj1":{"obj2":{"obj3":"val3","bool":true}}}},"ver":"1.0","number":2,"bool":true};

    var json2 = {"a":"val1","arry":["a","b","c"],"obj":{"obj1":"val1","obj2":"val2"},"arrObj":[{"artObj1":"arrObjVal1"},{"artObj2":"arrObjVal2"}],"ObjInsObj":{"ObjInsObj1":{"obj1":{"obj2":{"obj3":"val3","bool":true}}}},"ver":"1.0","number":2,"bool":true};

    var json3 = {"a":"1","b":"2"};
    var json4 = {"a":"1","b":"2","c":"3"};

    console.log("Json validation result "+comapareValidateJson.compare(json1,json2));//true
    console.log("Json validation result "+comapareValidateJson.compare(json3,json4));//true
    console.log("Json validation result "+comapareValidateJson.compare(json4,json3));//false

    console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json1,json2));//false
    console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json3,json4));//false
    console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json4,json3));//false

    var json5 = {"a":"val1","arry":["a","b","c"],"obj":{"obj1":"val1"},"arrObj":[{"artObj1":"arrObjVal1"}],"ObjInsObj":{"ObjInsObj1":{"obj1":{"obj2":{"obj3":"val3","bool":true}}}},"ver":"1.0","number":2,"bool":true};

    console.log("Strict Json validation result "+comapareValidateJson.deepStrictCompare(json1,json5));//true


    
### Methods
#### compare(json1,json2);
    compare all the keys and values in json1 against json2. If key is not present in json2 returns false, skips extra keys in json 2.
#### deepStrictCompare(json1,json2);
    compare all the keys in both json1 and json2. if any key is missing or the value is different in any of the json then returns false.

## License

This project is public domain. For more details, read about the [Unlicense](http://unlicense.org/).

---
_Source: https://npm.io/package/compare-validate-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
