1

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

Megajin
  • 2,648
  • 2
  • 25
  • 44
  • 1
    How exactly were you hoping to reference `John` when there are two of them there? Which one would show up if you did `my_object.John`. –  Oct 03 '16 at 13:02
  • https://www.google.com/search?q=site%3Astackoverflow.com+javascript+json+duplicate+keys –  Oct 03 '16 at 13:04
  • If you were wondering specifically about the logging, it's the same answer, except that NodeJS appears to automatically parse the JSON, which is pretty obnoxious. ...oh, it's because of `.require()`, which sees it as a JS program irrespective of the `.json` extension. –  Oct 03 '16 at 13:10
  • 1
    @squint thank you, I can't believe that I didn't realized that... 2 keys of course that's not possible...damn. – Megajin Oct 03 '16 at 13:10

2 Answers2

4

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
  }
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Yeah... very good response...! – Arj 1411 Oct 03 '16 at 13:02
  • 1
    Thank you for your answer. This helped me out. I can't believe that I was so stupid and didn't realize that I can't have the same key twice... guess that's what they call "Monday Tunnel Vision". Thanks again! – Megajin Oct 03 '16 at 13:08
1

It is not related to Node, it is general javascript principle that objects are augmented when ever you add a property to it.

const a = {};
a['Jhon'] = {'revisionDate':101};
a['Jhon'] = {'revisionDate':102};

console.log(a.Jhon);
//print: {''revisionDate':102}

This is cause the object property gets updated when you re-initialize it.

Nivesh
  • 2,573
  • 1
  • 20
  • 28
  • 1
    Thank you for your anser, Pointy was a little bit faster so he will get the accepted answer. Still I upvoted your answer. – Megajin Oct 03 '16 at 13:09