Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:19:15+00:00 2026-05-26T18:19:15+00:00

Is it possible to compare 2 sets of json objects for a difference? What

  • 0

Is it possible to compare 2 sets of json objects for a difference? What I have is a script thats polling for JSON object via jquery $post(). What I want to do is take the object that was just polled and compare it to a stored one. Where if there is any changes from one to the other apply them to the stored object or replace it (either way) but from the UI perspective I am working with seamlessly apply the changes to what the JSON object is for by finding the differences between the 2. I want to do this because right now I have it so the UI is completely reloading per poll regardless of change or not which basically looks like ** from a UX perspective.

I figure if I can find the differences if any between the 2 objects I would fire off a function that I would have edit the UI specific to the differences.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:19:16+00:00Added an answer on May 26, 2026 at 6:19 pm

    What I want to do is take the object that was just polled and compare it to a stored one. Where if there is any changes from one to the other apply them to the stored object or replace it (either way)

    If you would be happy with a really simple “Has it changed in any way? Yes/No” solution, where if it has changed you just replace the previous object with the new one (as per the part of your question that I quoted), then you could save the JSON response before you parse it, i.e., save it in the string format in which your web-server sends it. Then when the next response comes in compare the new string with the old string. If they are different (or if it is the first request) parse the JSON and process it for display as appropriate. Naturally this assumes that your server-side code is creating the JSON strings in a consistent format (and not, e.g., changing the order of the properties).

    If we assume you’ve already got (parsed) objects, an isEqual(a,b) function really should cope with nested objects, properties that are arrays, etc. This can be done recursively, and simply return true or false, but a getDifferences(a,b) function is going to get confusing in how it reports the differences within nested objects. Consider this simple example:

    old: {"mum" : "Maria", "dad" : "Pierre", "kids" : ["Joe", "Mike", "Louisa"] }
    new: {"mum" : "Julie", "dad" : "Pierre", "kids" : ["Joe", "Mary"] }
    

    Is the difference {"mum" : "Julie", "kids" : ["Mary"]}? The “mum” has changed, and the list of “kids” has changed, but has “Mike” changed to “Mary”, or are both “Mike” and “Louisa” gone with “Mary” being new, or…? Maybe it should be "kids": ["Joe","Mary"] because that’s the new value. How do you indicate the deletions? That’s just the first example off the top of my head where I don’t know how you would want to handle the differences. It could quickly get worse: what if the “kids” array contained objects instead of strings to represent a whole family tree? What if the new “mum” property was ["Maria", "Julie"] (to allow for step-parents and so forth)?

    If for your particular data you know you’ve only got one-dimensional objects then you can do something simple like the following:

    function getDifferences(oldObj, newObj) {
       var diff = {};
    
       for (var k in oldObj) {
          if (!(k in newObj))
             diff[k] = undefined;  // property gone so explicitly set it undefined
          else if (oldObj[k] !== newObj[k])
             diff[k] = newObj[k];  // property in both but has changed
       }
    
       for (k in newObj) {
          if (!(k in oldObj))
             diff[k] = newObj[k]; // property is new
       }
    
       return diff;
    }
    

    The simplest change to the above to allow for nested objects is to just assume that if a property is an object/array then you only care whether it is different in any way and not dig down to report exactly which “sub-properties” have changed. If so, simply take the above function and change:

    else if (oldObj[k] !== newObj[k])
    

    to

    else if (!isEqual(oldObj[k],newObj[k]))
    

    Where isEqual() is one of the many comparison functions floating around the web or on StackOverflow.

    (Note: I haven’t bothered with .hasOwnProperty() above because I assume that objects that were returned to an Ajax request as JSON will not be inheriting properties from a prototype chain. Similarly an isEqual() function for this purpose wouldn’t need to worry about properties being functions, it only needs to worry about what is valid in a JSON string.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Differences in string compare methods in C# Is there any difference between
Possible Duplicate: How to compare strings? ( == returns wrong value) I have the
I want to write a function that accepts two objects as parameters and compare
Possible Duplicate: php date compare I have a date that I take from the
Possible Duplicate: Compare 2 dates with JavaScript I would like to have a Javascript
Is it possible to compare two sounds ? for example app have already a
Is it possible to compare whole memory regions in a single processor cycle? More
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
I have thousands of large sets of tag cloud data; I can retrieve a
Possible Duplicate: Compare using Thread.Sleep and Timer for delayed execution I am considering whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.