npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

compare-validate-json

v1.1.1

Published

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

Downloads

2,033

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.

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.