Click here to Skip to main content
15,888,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have Two Json Strings that might have different values at some point of time , and I want to know if is it possible to compare them using JS?

For Instance,
I have these Two Json strings
JavaScript
//First Json
var Json1 = {"Name":"ABC";"Work":"Programmer";State:"123"};

//Second Json
var Json2 = {"Name":"XYZ";"Work":"Engineer";State:"456"};
Here, I want output like below:
Name value changed from 'ABC' to 'XYZ'
Work value changed from 'Programmer' to 'Engineer'
State value changed from '123' to '456'


What I have tried:

Not able to find the solution. Can you please help here?
Posted
Updated 23-Nov-19 2:48am
v4
Comments
Richard MacCutchan 6-Jan-17 6:55am    
The obvious way would be to parse each string to an object and then compare the fields.

Firstly, what you have is not valid JSON. You use semicolons instead of commas. Also in a JSON string, all keys have to be surrounded by quotes (which is not the case in your example).

Your first code step would be to convert the JSON string to an object, using JSON.parse. Then you can use Object.keys to get all keys from the first object, and you can loop over these keys to see the difference in values in the two objects.
JavaScript
var jsonString1 = '{"Name":"ABC","Work":"Programmer","State":"123"}';
var jsonString2 = '{"Name":"XYZ","Work":"Engineer","State":"456"}';

var jsonObject1 = JSON.parse(jsonString1);
var jsonObject2 = JSON.parse(jsonString2);

var keys = Object.keys(jsonObject1);
for (var i = 0; i < keys.length; i++) {
  var key = keys[i];
  if (jsonObject1[key] != jsonObject2[key]) {
    console.log(key + " value changed from '" + jsonObject1[key] + "' to '" + jsonObject2[key] + "'");
  }
}
 
Share this answer
 
Comments
45678910 9-Jan-17 6:36am    
Thank You for your quick look.
But I am getting the below output. Not the I am expecting.

9 value changed from 'A' to 'X'
10 value changed from 'B' to 'Y'
11 value changed from 'C' to 'Z'
....
....

Can you please help me?
Thomas Daniels 9-Jan-17 10:19am    
For me it worked. Do you want to share your current code in a JSFiddle so I can check it out?
45678910 13-Feb-17 7:34am    
Hi,
Sorry to late response. It worked for me as well. I did small mistake. After I corrected it, it works fine. Thanks Very much for your help.
// You Can Use loops to get result exactly as you want
const Json1 = {Name: 'ABC', Work: 'Programmer', State: '123'};
const Json2 = {Name: 'XYZ', Work: 'Engineer', State: '456'};

const obj1Keys = Object.keys(Json1);
const obj1Values = Object.values(Json1);
const obj2Keys = Object.keys(Json2);
const obj2Values = Object.values(Json2);

for ( let i = 0; i < obj1Keys.length; i++ ) {
  for ( let j = 0; j < obj2Keys.length; j++ ) {

    if ( obj1Keys[i] === obj2Keys[j]) {
      if ( obj1Values[i] !== obj2Values[j]) {
          console.log( `${obj1Keys[i]} value changed from
                           '${obj1Values[i]}' to '${obj2Values[j]}'`);
      }
    }
  }
}
 
Share this answer
 
v4
Comments
Richard Deeming 27-Nov-19 14:54pm    
That's the same as solution 1. You've not added anything to the discussion. And by copying someone else's answer, you look like a plagiarist.
Member 14665733 4-Dec-19 22:21pm    
Mr. Richard
May be you are a Rote memorizer !
Can you see that the first solution is not giving exactly that answer which one is expected in question description.
And also first solution is not following the current version js or ts lint.
If adding something for discussion is making some confusions then nothing for discussion is better more then adding something for discussion.
if 2 + 2 = 4 then always 2 + 2 = 4.
I am not plagiarist but you look like a blind with perfect eyes.
Richard Deeming 5-Dec-19 6:41am    
It may not be exactly the same code, but it is effectively the same suggestion - use Object.keys to get the keys, walk through the list, and compare the values.

I never said you were a plagiarist. I was simply pointing out that you haven't really added anything to the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900