Megajin Megajin - 2 months ago 8
JSON Question

JSON duplicates won't show up in console.log()

I have a pretty flat JSON which can have an attribute more than once. After I've built some logic to kick out the 'old' values, I wanted to try it out of course. Then I discovered something really strange. My logic always counted the attributes once which meant there are no duplicates. So I went ahead and created this dummy JSON:

{
"John": {
"id": 10001,
"name": "John Doe",
"iconId": 10,
"level": 12,
"revisionDate": 1000000000001
},
"John": {
"id": 10001,
"name": "John Doe",
"iconId": 10,
"level": 12,
"revisionDate": 1000000000005
},
"Amy": {
"id": 10002,
"name": "Amy Smith",
"iconId": 15,
"level": 11,
"revisionDate": 1000000000001
}
}


As you can see the duplicate here is John. The only difference is his revision date.

Now I load up the JSON in NodeJS and log it like this:

// loading JSON
let oldResults = require('./dummy.json');
// log JSON
console.log(oldResults);


At this point the strange thing happens. My console will show me the second John in my JSON and Amy, but never the first John:

λ node debug.js
{ John:
{ id: 10001,
name: 'John Doe',
iconId: 10,
level: 12,
revisionDate: 1000000000005 },
Amy:
{ id: 10002,
name: 'Amy Smith',
iconId: 15,
level: 11,
revisionDate: 1000000000001 } }


If I change the second John to John123 the first one will be logged as the others (doesn't matter If I change the first or the second, the results will be the same):

λ node debug.js
{ John:
{ id: 10001,
name: 'John Doe',
iconId: 10,
level: 12,
revisionDate: 1000000000001 },
John123:
{ id: 10001,
name: 'John Doe',
iconId: 10,
level: 12,
revisionDate: 1000000000005 },
Amy:
{ id: 10002,
name: 'Amy Smith',
iconId: 15,
level: 11,
revisionDate: 1000000000001 } }


I've tried this with the normal Windows CMD and CMDER. Both show me the same result. Also I cleared the node cache, still no success... I load this JSON in my debug.js with no other logic, which could alter the JSON.

Can someone please explain me why NodeJS is kicking out the second John?

Regards, Megajin

Answer

A given property name can only exist once in a given object. If you create an object with an object initializer:

var o = { a: "hello", a: "world" };

then you end up with just a single property called "a". (In strict mode, you end up with an error.)

When JSON is parsed, you have the same problem: the JSON parser incrementally constructs the object graph, and a repeated property name will overwrite any previously-set value for that property.

If you have some structure that you want to serialize as JSON, and the structure has properties with multiple values, probably the best way to do that is by giving the property an array as its value:

{
  "John": [
    {
        "id": 10001,
        "name": "John Doe",
        "iconId": 10,
        "level": 12,
        "revisionDate": 1000000000001
    },
    {
        "id": 10001,
        "name": "John Doe",
        "iconId": 10,
        "level": 12,
        "revisionDate": 1000000000005
    }
  ],
  "Amy": {
      "id": 10002,
      "name": "Amy Smith",
      "iconId": 15,
      "level": 11,
      "revisionDate": 1000000000001
  }
}