0
json1 = 
[{
        "name": "apple"
},
{
        "name": "mango"
},
{
        "name": "orange"
}]

json2 = [{
        "name": "apple"
},
{
        "name": "mango"
}]

I need to compare the two jsons and find out the mismatch between two json-arrays. The expected result is obviously orange. Would you please anyone help me getting this done.

2
  • 1
    What you posted are two JavaScript arrays, not JSON. JSON is a textual, language-independent data format, just like XML. Commented Apr 9, 2015 at 17:55
  • You can use underscore.js for comparing. For more, stackoverflow.com/questions/24336485/… Commented Apr 10, 2015 at 3:04

2 Answers 2

1

Grab json-diff from npm - https://www.npmjs.com/package/json-diff

Or, just have a look at the source code and do whatever he did. - https://github.com/andreyvit/json-diff

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

1 Comment

The OP seems to have JavaScript arrays, not JSON.
1

There are some packages out there on npm that appear to do this (https://github.com/NV/objectDiff.js).

If efficiency is not a concern, you can just do a scan, where you loop across json1, and for every element see if it exists in json2, and then vice versa. In this case, since you're only looking for these name KVPs, you could also just normalize it into ['apple', 'mango', 'orange'] and ['apple', 'mango'] and use indexOf().

Comments

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.