views:

127

answers:

4

On JSON.org the essential data structures that JSON represents are given as

  1. A collection of name/value pairs, and
  2. An ordered list of values.

I have not been able to find anywhere whether a second member having the same name as one already parsed into the current object should (a) throw an exception or (b) replace the existing member.

Is this specified anywhere?

What do existing parsers do with repeated names?

EDIT: I am looking to define correct behavior for my parser.

A: 

Because JSON is simply a subset of Javascript, it largely depends upon the Javascript specification. I don't know personally what the answer is, but I would highly suggest not relying upon the behavior if at all possible.

NilObject
+1  A: 

They last name found by the parser is replaced by the new one. It doesn't throw an expection.

It is simply a Javascript syntax thing.

var json = {};

// lets augment the object
json.one = 1;
json.one = 2; // it gets replaced
Luca Matteis
+3  A: 

JSON is simply a subset of the object literal notation of JavaScript and as such, is constrained by the same rules - the latest value for repeated keys will override any previously assigned value for that key, within the specific object. Think in terms of assigning a value to an object property; A later assignment will override an earlier one.

To demonstrate this, I have set up an example here. The code is displayed on the page, and as can be seen, the messagebox has the name 'Barney' in it.

Code here -

$(function() {

    $('#myButton').click(function(e) 

    {

    var myJsonString =  "Person = {'firstName':'Fred','lastName':'Flintstone','firstName':'Barney'}";

    eval("(" + myJsonString + ")");

    alert(Person.firstName);

    });

});

By the Way, I have used eval() here for ease of use. I would recommend using a JSON parser instead of eval() due to security issues.

Russ Cam
I agree about not using eval(). I actually use JSON in Java and therefore purely as a data encapsulation, not including code.
Software Monkey
BTW, thanks for an excellent response; I feel guilty for not thinking to simply test this in JS myself... problem with programming almost exclusively in Java, I guess.
Software Monkey
No problem. I highly recommend http://jsbin.com/ for testing snippets of HTML, CSS and JavaScript. It allows you to easily include common JS libraries and save your code to a public URL. Combined with firefox, it is an absolute gem to test ideas with
Russ Cam
A: 

I am pretty sure that both behaviors you list would be accepted, along with others (use the first one, use any of them). :-) That is, such behavior is undefined from JSON specification POV.

As a practical matter, implementations I have used do either one of suggestions you mentioned, or "use the first one".

So no, I would not count on specific behavior given that tools can choose what to do.

StaxMan
Yeah, I ended up making my parser throw an exception, as default behavior, but I added a parsing option which treats the repeated keywords as an array; am considering an alternate option for stipulating the replace behavior instead.
Software Monkey