3

I have the following JSON String.

var jsonString = '{"J":4,"0":"M", "J":5,"0":"N"}'

If I parse it using jquery.parseJSON(jsonString) I get

{"J":5,"0":"N"}

Questions

1) Is jsonString malformed?

2) Is there another jquery method I can use to include objects with duplicate keys, i.e.

jquery.someAwesomeMethod(jsonString) => {"J":4,"0":"M", "J":5,"0":"N"}
4
  • 1
    How are you going to access the data of this JSON? Commented Aug 3, 2011 at 16:32
  • 2
    Given the code obj.J what would you expect it to evaluate to? Commented Aug 3, 2011 at 16:32
  • In your example, jsonString is a JavaScript object, not a JSON string. There is not reason to call jquery.parseJSON(jsonString). And as already said by others, object property names are unique in JS. Commented Aug 3, 2011 at 16:38
  • @Felix King: Woops, it should be in single quotations. I've edited the post. Commented Aug 3, 2011 at 17:53

3 Answers 3

3

To answer your questions directly:

1) Is jsonString malformed?

It may actually be legal JSON (I'm not totally sure), but it will not be practical when used in any sort of javascript context or when parsed with any javascript JSON parser like jquery.parseJSON because of the duplicate keys.

2) Is there another jquery method I can use to include objects 
   with duplicate keys, i.e.

jquery.someAwesomeMethod(jsonString) => {"J":4,"0":"M", "J":5,"0":"N"}

No, there is no such jQuery method both because jQuery doesn't have that and because the output you desire is not possible in javascript. You've represented a Javascript object syntax, but Javascript objects don't support duplicate keys. In Javascript, the last value set for a given key wins.

So, if you intend to parse the JSON into a normal javascript object (how JSON is normally used in a browser app and how it is parsed with jquery.parseJSON()), you will not get duplicate keys with that type of data declaration as later declarations of the same key will likely just replace earlier declarations - only one will survive.

You probably want some different type of data structure such as an array or an object with array values for the keys:

Here's an array that just alternates between keys and values in pairs:

[
    "J", 4, 
    "0","M", 
    "J", 5, 
    "0","N"
]

obj[0] // key
obj[1] // corresponding value

When accessing the array, the even indexes would be keys, the odd indexes would be values.

Or here's an object where the values are arrays so you can have more than one value per key:

{"J":[4, 5], "0":["M", "N"]}

typeof obj["J"]  // Array
obj["J"].length  // array of length == 2
obj["J"][0]      // first value in array == 4
obj["J"][1]      // second value in array == 5

Each key would hold an array of values.

Sign up to request clarification or add additional context in comments.

2 Comments

Javascript != JSON, so what javascript does is only of interest when using JSON from Javascript. Spec does not say what should be done with multiple keys, although from implementation perspective "last one wins" is one common choice.
@StaxMan - This particular question is about using JSON from Javascript. They specifically ask about using jquery.parseJSON() and that's how I have addressed my answer.
1

Each key of a JSON string must be unique or else it will get overridden with the newest instance of the key when you parse it into a javascript object as you are doing.

2 Comments

JSON != JavaScript object. I can have as many keys with the same name as I want in a JSON string, without making the previous ones magically disappear. Only parsing the JSON into a JavaScript object will overwrite duplicate keys (in the JavaScript object).
@Felix -- when you turn it into a javascript object as the OP does, the keys overwrite each other.
1

JSON is a subset of Javascript (ECMAScript), and an object literal in Javascript is evaluated as an object creation followed by property assignments, so duplicate property names are allowed but the last of the duplicates overwrite the previous.

Besides, if you would parse the JSON representation of an object in Javascript and preserve the duplicate names, you would need to represent the result as something other than a Javascript object, which would be impractical.

3 Comments

No. JSON is NOT defined as a subset of Javascript (see the spec), although in practice it can be viewed as such. But it is important to remember distinction because otherwise one might think behavior would follow JS spec, which it does not. Rather, behavior for this case is strictly undefined.
@StaxMan: Well, more specifically JSON "is based on a subset of the JavaScript Programming Language". If you don't fall back on the Javascript specification, there is a lot left undefined...
Yes, quite a few things are undefined outside of use within JS. But since this question is for use within JS I guess it's fair enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.