2

I am trying to compare 2 JSON API responses in terms of structure, and ignoring the data.

Tried converting the JSON responses into Objects using Jackson implementing the following code, but it does not give me the expected results

MyObject ob = new ObjectMapper().readValue(jsonOutput, MyObject.class);

What could possibly be the best way to compare 2 JSON API responses for structure and ignoring the data.

14
  • What do you mean by structure exactly? Commented Feb 16, 2015 at 16:06
  • What is the expected result for a given JSON? Commented Feb 16, 2015 at 16:06
  • That absolutely depends on why you want to compare the structure of two JSON objects. What should the result of such an comparison express? Commented Feb 16, 2015 at 16:06
  • 1
    Well then why don't you use JSON Schema? Commented Feb 16, 2015 at 16:15
  • 1
    Yes there is; however you will only get half the picture... Commented Feb 16, 2015 at 16:22

3 Answers 3

4

Not sure if this is what you want but you have the solution of using this package. It can generate a diff out of two JSONs.

The result will be a JSON Patch (ie, RFC 6902). You can track object member deletions and removals since they will always be either add or remove operations.


EDIT from the comment (which is why it is important to fully specify the question!), it seems that what you want is a JSON Schema instead; I also happen to have an implementation which uses Jackson...

Sign up to request clarification or add additional context in comments.

3 Comments

This looks more like what I was looking for. I'll check this out and update the space here
The other solution is to walk the JSON using jackson-coreutils which implements JSON Pointer, and collect the field differences; however this would be a double walk if you want the complete picture.
I would be ok with a O(N2) for now as long as it gets me the field differences
1

Given the ad-hoc nature of JSON, this simple feature isn't easy, depending on how complex the structure is. If each API just returns a single object with primitive values (null, "" string, boolean or a number), then you can collect the keys in a sorted set and compare them.

But if the JSON becomes deeply nested, this becomes more and more complex. Maybe you can write a printer for JSON which just prints the the field/key/property names sorted without the values and the JSON structure (object and array). You can then put that into a string and compare the two.

3 Comments

A recursive-descent tree walk would not become any more complex if the JSON is "deeply nested".
@HotLicks: It becomes more complex for a human mind to understand the differences, for example when child nodes are moved in the tree.
If child nodes are moved then it's not the same tree.
1

Parse both JSON strings into Maps and Lists. Then write a recursive-descent tree walker that compares each element, using whatever criteria you want for the comparison. The structure of the tree walker would be essentially the same as a JSON serializer, so one could start with an open-source serializer. Could probably be done in about 300-500 lines of code.

Of course, one does need to define what "matches" means. If one array has 5 elements and another 10 it's easy to say they "match" if they both are only strings or only numeric, but if they are arrays of objects then it gets messier, if you want to allow differing array counts to "match".

1 Comment

So in this case each element of the structure would be the key in the map and then compare it against the other map. Is my understanding right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.