Skip to content

Compare JSON objects for structure equality, despite the actual content

License

Notifications You must be signed in to change notification settings

fgarci03/json-structure-diff

Repository files navigation

json-structure-diff

Compare JSON objects for structure equality, despite the actual content

Installation

npm install --save json-structure-diff

or

bower install --save json-structure-diff

Usage

NPM:

var compare = require('json-structure-diff').compareJSONObjects;

var obj1 = {
  parent: 'nameOfObject',
  content: {
    whatever: 'you',
    want: {
      here: '!'
    }
  }
};

var obj2 = {
  parent: 'nameOfComparedObject',
  content: {
    whatever: 'you',
    want: {
      here: {
        is: 'cool',
        right: '?'
      }
    }
  }
};

var objectsToCompare = [obj1, obj2];

var errors = compare(objectsToCompare, options);

BOWER:

<script src="json-structure-diff/index.js"></script>
<script>
  jsonStructureDiff.compareJSONObjects([oj1, obj2], options);
</script>

This function accepts an array of objects to be compared, and returns a list of errors if they exist, or null if they don't. It checks strictly for JSON structure without looking at the contents. It does check if the typeof variable matches!

The objects need to have 2 properties:

  • parent: The name of the object, to use on the error list;
  • content: the actual content of the object to compare.

Optionally, you can pass an 'options' object with a humanReadable boolean property. If true, the return value of the method is an array of strings (['nameOfObject > want.here is string and nameOfComparedObject > want.here is object']), else it's an array of objects:

[
  {
    parent: 'nameOfObject.want.here',
    comparedParent: 'nameOfComparedObject.want.here',
    typeOfParent: 'string',
    typeOfComparedParent: 'object'
  }
]

Default value is false.

Credits

Filipe Garcia

License

ISC

TODO

About

Compare JSON objects for structure equality, despite the actual content

Resources

License

Stars

Watchers

Forks

Packages

No packages published