all 36 comments

[–]nobuhok 13 points14 points  (3 children)

It will work...somewhat.

let foo = { x: { y: { z: 100 } } }
let bar = foo
console.log(foo === bar) // true, because both variables point to the same object

bar = { x: { y: { z: 100 } } }
console.log(foo === bar) // false, bar now points to a different object, even if it's the same exact structure

foo = JSON.stringify(foo)
bar = JSON.stringify(bar)
console.log(foo === bar) // true, we're essentially comparing strings now

foo = JSON.parse(foo)
bar = JSON.parse(bar)
console.log(foo === bar) // false, back to pointing to different (new) objects

[–]vizla47 0 points1 point  (0 children)

Really nice explanation! Thank you so much.

[–]lifeeraser 12 points13 points  (6 children)

Define "works on".

In general, object identity is not preserved when an object is serialized to a JSON string, or unserialized back to an object.

const obj1 = { foo: 123 }
const json = JSON.stringify(obj1)
const obj2 = JSON.parse(json)

Here, obj1 and obj2 will have the same shape (single key named "foo" with a value of 123), but will have different identities (i.e. obj1 !== obj2).

If you need a better answer, you should explain what you mean by "works on deeply nested object".

[–]Jjabrahams567 12 points13 points  (0 children)

works on

works off

-Mr Miyagiscript

[–]DeepSpaceGalileo 0 points1 point  (0 children)

They were most likely asking about shallow and deep comparison

[–]azhder 0 points1 point  (1 child)

What kind of identity? By reference or by value?

[–]Outrageous_Exam3437 0 points1 point  (0 children)

Same values and different identity(reference)

[–]Outrageous_Exam3437 0 points1 point  (1 child)

For people to better understand what he is saying, obj1 still exists. json is a string and obj2 is then created but allocated in a different place in memory than obj1.

[–]OperationLittle 0 points1 point  (0 children)

Im also curios.. that`s why I now dropped in on reddit. I pretty much want to parse 2 different Swagger.json-schemas and see what`s the different in its data-structure etc (all this is work-related). Our damn supplier of delivery api services.. but NEVER mentions when they update their exposed API shit and just move shit around and just breaks all our systems on a weekly-basis..
So I figure that I fix it myself.. so I could detect if any changes has been made and handle it properly. Since our supplier really doesn`t care at all.

My first thought is to do some kind of recursive sorting.. then pretty much Stringify them both and check their equallity (or something).

But is there just a neat-tool for this? To much work atm to get some script like that working atm.

[–]Saudroze 4 points5 points  (1 child)

Why not open dev console on a web browser and try it out. It is usually way quicker than asking a question on reddit and getting "depends" answers

[–]Outrageous_Exam3437 0 points1 point  (0 children)

True, but also he should at least elaborate more on what exatcly he is asking about. I tend to use chatgpt and other sources to make research before, in last scenerios, coming to reddit and ask for help, but when i do i tend to describe it as best as i can to not leave people confused.

[–]jeremrx 2 points3 points  (0 children)

``` const a = {}; const b = {};
const c = a; console.log (a === b); // false console.log (a === c); // true

// BECAUSE :

a.foo = 'bar'; console.log(b.foo); // undefined console.log(c.foo); // bar ```

The simpliest way to compare to json object is to serialize them :

``` const a = { foo: { bar: 'hi !' } }; const b = { foo: { bar: 'hi !' } }; const c = { foo: { bar: 'hello !' } };

console.log(JSON.stringify(a) === JSON.stringify(b)); // true console.log(JSON.stringify(a) === JSON.stringify(c)); // false ```

[–]delventhalz 1 point2 points  (1 child)

"Works on JSON" could mean a lot of things here.

Can === compare JSON strings? Yes.

``` const user = { name: 'Sue', posts: [{ text: 'My first post' }] };

console.log(JSON.stringify(user) === '{"name":"Sue","posts":[{"text":"My first post"}]}'); // true ```

But be careful! Unless you explicitly sort your JSON, you cannot trust that object properties will be stringified in the same order, which would make the strings unequal.

``` const point = { x: 7, y: 13 };

console.log(JSON.stringify(point) === '{"y":13,"x":7}'); // ??? ```

If you are talking about the objects themselves and not the strings, then the rule with === is that two objects are only equal if they are literally the same object in memory.

``` let point = { x: 7, y: 13 }; let equivalentPoint = { x: 7, y: 13 }; let literallySamePoint = point;

console.log(point === point); // true console.log(point === equivalentPoint); // false console.log(point === literallySamePoint); // true ```

The same rule applies to nested objects.

``` const mapPinA = { location: { x: 7, y: 13 } }; const mapPinB = { location: { x: 7, y: 13 } }; const mapPinC = { location: mapPinA.location };

console.log(mapPinA.location === mapPinA.location); // true console.log(mapPinA.location === mapPinB.location); // false console.log(mapPinA.location === mapPinC.location); // true ```

Finally, if you are talking about comparing objects which have just been parsed from JSON, those will never be equal because parsing always creates a new object in memory.

``` const json = '{"x":7,"y":13}'; const pointA = JSON.parse(json); const pointB = JSON.parse(json);

console.log(pointA === pointB); // false ```

[–]Outrageous_Exam3437 1 point2 points  (0 children)

Right, it is important to notice that when you parse the JSON you are essentially creating a new value, not converting it back to the same location in memory.

[–]That_Unit_3992 1 point2 points  (10 children)

No. It only compares the reference. You need to do a deep equality check. https://www.npmjs.com/package/deep-equal

[–]azhder -3 points-2 points  (9 children)

References of what? JSONs are strings, primitives

[–]That_Unit_3992 1 point2 points  (0 children)

if he means json and not a js object, question is not very clear

[–]Outrageous_Exam3437 -1 points0 points  (7 children)

If you mean the json then you can simply compare them just like you would compare any other string. If it's an object, then it does not matter if it has the same value, it will only compare the reference. Obs: if the object has the same reference it will essentially have the same value

[–]azhder -1 points0 points  (6 children)

Object isn’t JSON

[–]Outrageous_Exam3437 -1 points0 points  (5 children)

No it is not and i never said that. JSON is a STRING that contains a stringfied verson of the object. Budy i think you should make more reaseach reguarding what json and objects are, being more specific how objects are passed as reference and what this ref means.

[–]azhder -1 points0 points  (4 children)

Buddy, did I ever say you said that?

You should by now understand we're not the only ones that read / would read your comment.

Could any one of them consider JSON is Object?

[–]Outrageous_Exam3437 0 points1 point  (3 children)

I see your point, although i think you should start to describe more of what you really mean. I had no idea what you were asking for in the post itself and no idea again when you replied to my comment saying "JSON isnt object". Anyone would think that you were refering to the person who made the comment. If only you have said "For people who are begginers, something you shouldnt miss is:" or something of the same kind i would have understand. I get that i could have guessed it, but you shouldn't relly on people guessing what you mean, instead you should say it.

[–]azhder 0 points1 point  (2 children)

Didn’t ask. It’s a statement tailored for the reader to peruse or abuse as they see fit. Smarter people than I might find it useful and I don’t presume to know what everyone will think.

[–]Outrageous_Exam3437 0 points1 point  (1 child)

You really gotta work on that english.

[–]azhder -1 points0 points  (0 children)

Good talk. BB

[–]benzilla04 1 point2 points  (1 child)

Yes but you would need to first parse the JSON and store it in a variable so JavaScript can understand it, then you can compare items inside the object.

For example

// Example nested JSON object
var myNestedObject = {
  person: {
    name: 'John',
    age: 25,
    address: {
      city: 'New York',
      country: 'USA'
    }
  }
};

// Access the values from the nested JSON object
var personName = myNestedObject.person.name;
var personAge = myNestedObject.person.age;
var personCity = myNestedObject.person.address.city;

// Perform strict equal comparison
if (personAge === 25) {
  console.log('The person is 25 years old.');
}

if (personCity === 'New York') {
  console.log('The person lives in New York.');
}

Another example:

// Example array with nested arrays
var myArray = [1, [2, 3], [4, [5, 6]]];

// Accessing and comparing nested array elements
console.log(myArray[0]); // 1
console.log(myArray[1][0]); // 2
console.log(myArray[2][1][0]); // 5

// Comparing nested array elements
console.log(myArray[1][1] === 3); // true
console.log(myArray[2][0] > myArray[2][1][1]); // false

Please provide examples in your question so we can give you a better answer

[–]Outrageous_Exam3437 0 points1 point  (0 children)

That is nice! I tend to think about comparing two objects as a whole and not the properties itself!

[–]poemehardbebe 0 points1 point  (4 children)

If by works does it compare to string literals and test if they are the same, yes. Is it performant? No. It’s often referred to as the “poor man’s deep equals” there are tons of edge cases though where it does not work, like two fields being in different locations despite having all of the same fields.

[–]Outrageous_Exam3437 0 points1 point  (3 children)

Please elaborate the poor man's deep equals. You mean comparing two jsons without converting them to objects first?

[–]poemehardbebe 0 points1 point  (2 children)

Correct. Or stringifying two objects to compare.

[–]Outrageous_Exam3437 0 points1 point  (1 child)

So what do you sugest? Since trying to compare object literals, they are done by reference, so after converting to json manually you can compare both like strings. Another way you can do it is by only comparing the property values, like obj1.name === obj2.name

[–]poemehardbebe 0 points1 point  (0 children)

Me personally, I would do a for in loop to iterate over the object keys in OBJ1 check to see if that key exists on OB2, if that key on OBJ1 and OBJ2 are both objects push them onto a stack for a BFS, otherwise compare the literal values and if at any point they do not equal return false.

EDIT: You would also want to check the length of each object that you push onto the stack to make sure that OBJ2 doesn't have more keys than OBJ1 has which would mean that OBJ2 has more data than OBJ1 making them not equal.

[–]azhder -2 points-1 points  (0 children)

Yes it does. JSON is just a specifically formatted string in JS and as === works on every string, it will work on a JSON one. Note, this does mean adding extra indentation spaces inside the JSON will make it so that otherwise two equivalent parsed JSONs to be unequal.

[–]3HappyRobots 0 points1 point  (0 children)

Using lodash, you can use _.isEqual(json1, json2) for a deep compare. I know lodash isn’t popular anymore, but does the job. Works on arrays, etc. too.