Giter VIP home page Giter VIP logo

jsonequals's Introduction

JsonEquals

CircleCI Release

A simple, flexible JSON deep-equality comparator with optional settings to ignore and prune specific JSON fields.

Overview

JsonEquals is a simple JSON deep-equality comparator for Java. It parses two JSON inputs and does a thorough comparison between each key, value, and array index, ignoring the ordering of JSON keys during comparison. In addition, one can selectively define what equality consists of by providing certain fields to ignore and certain array indices to prune (see examples below).

Example use cases: JsonEquals is perfect for comparing JSON responses between production and staging environments when your project is due for an API upgrade.

The current version of JsonEquals is used in production under the same scenario listed above, to compare JSON responses between different environments, which helps accelerate the upgrade process for updating API dependencies to newer versions by indicating differences in response data.

Javadocs here

Usage

JsonEquals uses LazyJSON, a simple and lightweight Java library to parse (read) JSON.

To compare two JSON strings, simply create JsonRoot objects: JsonRoot.from(jsonString) and use JsonRoot#compareTo(another JsonRoot)

The JsonRoot#compareTo method returns a JsonCompareResult, which holds information regarding the comparison (isEqual boolean, success messages list, failure (inequality) messages list)

Settings

JsonEquals is capable of ignoring fields and pruning JSON objects within JSON arrays before comparison. For example, responses with different timestamps can be ignored (ignore list), and array objects containing JSON objects with certain field values can be ignored (pruned list with expected value to be filtered out).

Ignoring JSON Fields

Supply a List<String> of strings in a dot-notated JSON path format to have JsonEquals ignore these nodes during comparison. Use JsonRoot#compareToWithIgnore()

List<String> ignoreList = new ArrayList<>();

ignoreList.add(... see below comment block);
/*
ignoreList.add("$");    // Ignores root element and all sub-children
ignoreList.add("$[1]"); // If root element is an array, ignore the second object in the array including all its sub-children
ignoreList.add("$[*]"); // Use * as a wildcard to specify all elements in an array
ignoreList.add("$.data.timestamp");
// Ignores the root -> data -> timestamp values during comparison, e.g. the two JSONs below will be equal
{
    "data": {
        "name": "John Smith"
        "timestamp": 12345
    }
}

    versus
    
{
    "data": {
        "name": "John Smith"
        "timestamp": 23456
    }
}
*/

JsonCompareResult result = jsonRootA.compareToWithIgnore(jsonRootB, ignoreList);

Pruning JSON Arrays

Supply a Map<String, String> in a dot-notated JSON path format to expected value in string form to act as a predicate. Any matches will be filtered out (read: removed) before the comparison starts, and therefore ignored during comparison. Note that this will shift the array indices. Use JsonRoot#compareToWithPrune()

Format: Map<String, String> -> ("arrayIndexObjectPath:fieldName", "valueToFilterInStringForm")

e.g.

    Map<String, String> pruneMap = new HashMap<>();
    pruneMap.put("$.someObject.someArray[*]:booleanName", "false"); // * is a wildcard to select all array elements
    
    JsonCompareResult result = jsonRootA.compareToWithPrune(jsonRootB, pruneMap);
    // Or combine both pruning and ignore list
    JsonCompareResult ignoreAndPruneResult = jsonRootA.compareTo(jsonRootB, ignoreList, pruneMap);

the JSON string:

{
    "someObject": {
        "someArray": [
            ... // Objects from index 0 to 5 go here
            {   // Object with index 6 in someArray
                "someString": "hello",
                "booleanName": "false"
            },
            {   // Object with index 7 in someArray
                "someString": "world",
                "booleanName": "true"
            }
            ... // Objects from index 8 and beyond go here
        ]
    }
}

In the above example, after pruning the JSON file before comparison, the object $.someObject.someArray[6] will be removed from comparison, which shifts object $.someObject.someArray[7] down to index 6, and so on, until everything that matches has been pruned from the array. This can be useful when checking a list of responses from two different environments where there can be gaps in the arrays, for example, if we have an array of applications installed, we can define equality as having the same installed apps from both responses by pruning the apps that are not installed.

For a thorough example, see IgnoreAndPruneTest.java, along with ignore_prune_a.json and ignore_prune_b.json

Debug Mode

Debug mode can be enabled with JsonEquals.setDebugMode(true), which will continuously log each leaf object or array primitive value being checked to the console.

See Examples

Check out the test files for examples.

Installation

JsonEquals uses JitPack for distribution. See https://jitpack.io/#kvnxiao/jsonequals for more information.

Replace @VERSION@ with the version number or commit hash.

Gradle

    allprojects {
        repositories {
            jcenter()
            maven { url 'https://jitpack.io' }
        }
    }
    dependencies {
        compile 'com.github.kvnxiao:jsonequals:1.0.1'
    }

Maven

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    <dependency>
        <groupId>com.github.kvnxiao</groupId>
        <artifactId>jsonequals</artifactId>
        <version>1.0.1</version>
    </dependency>

Contributing

Have suggestions? See problems? Got new ideas or improvements? Feel free to submit an issue or pull request!

License

This project is licensed under Apache 2.0

jsonequals's People

Contributors

kvnxiao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsonequals's Issues

Ignore Order

Hi All,
I am new to this, i am using this to compare two json string. Inside json string it is having multiple array and the order is not fixed on the second json. Both value is coming form different source.
How can i compare the string and ignore the order same like JSONCompareMode.LENIENT.

Please suggest

How can i get difference from two JSON files?

I have two JSON files and trying to compare it with you given example ComparisonTest.java but I can see only build the successful message. I am new to Gradle project can you please help?

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.