22
var myObj = {"suppliers":[{"name":"supplier1","12m":"0.08","24m":"0.06"}]};

alert(myObj.suppliers[0].12m);

Is there a different way to get this property, or should I just not use a key that starts with a number?

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
bodine
  • 1,763
  • 4
  • 19
  • 28
  • 2
    BTW, this is not JSON...it is an object, technically speaking. – mattsven Apr 27 '11 at 20:28
  • @NeXXeuS Could you clarify that commment? I thought Javascript Object Notation was the way objects were defined. Are these terms not synonymous? Thanks – bodine Apr 27 '11 at 21:19
  • In essence, they are the same, but in javascript you call them objects. Plus, in JSON all keys have to be qouted: `{ "key":123 }`. – mattsven Apr 27 '11 at 21:22
  • @NeXXuS My Object is declared using 'javascript object notation' is it not? All my keys have double quotes, do they not? I'm not seeing the distinction you are making. – bodine Apr 27 '11 at 21:25
  • 1
    @bodine: I like [this explanation](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). It's just the context in which you're using the object notation. – Cheran Shunmugavel Apr 28 '11 at 05:48
  • @Cheran S Okay, this makes sense. In my actual code, the string I'm providing to myObj is coming from an ajax call, though that was not pertinent to my question. Thank you for clearing this up. I'm still not sure why this turned into a discussion of the JSON specification. The tag is appropriate to the question. – bodine Apr 28 '11 at 14:54
  • @mattsven JSON is an object literal expressed as a string. Stands for JavaScript Object Notation. They are literally one in the same. – I wrestled a bear once. Aug 25 '14 at 19:26
  • JSON is a data format based on JavaScript literal format. Unless it is encoded as a string, if "JSON" is in a JavaScript file then it is just JavaScript literals. – Quentin Aug 25 '14 at 22:08
  • Related: [JavaScript property access: dot notation vs. brackets?](/q/4968406/4642212). – Sebastian Simon Jan 29 '22 at 06:19

2 Answers2

43

You can use the following syntax to do what you describe using bracket notation:

myObject["myProperty"]

Bracket notation differs from dot notation (e.g. myObject.myProperty) in that it can be used to access properties whose names are illegal. Illegal meaning that with dot notation, you're limited to using property names that are alphanumeric (plus the underscore _ and dollar sign $), and don't begin with a number. Bracket notation allows us to use a string to access a property and bypass this.

myObject.1 // fails, properties cannot begin with numbers
myObject.& // fails, properties must be alphanumeric (or $ or _)

myObject["1"] // succeeds
myObject["&"] // succeeds

This also means we can use string variables to look up and set properties on objects:

var myEdgyPropertyName = "||~~(_o__o_)~~||";

myEdgyObject[myEdgyPropertyName] = "who's there?";

myEdgyObject[myEdgyPropertyName] // "who's there?";

You can read more about dot and bracket notation here, on MDN.

mattsven
  • 22,305
  • 11
  • 68
  • 104
1

Yes, use bracket syntax:

alert(myObj.suppliers[0]["12m"]);

From MDN

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

cmd
  • 11,622
  • 7
  • 51
  • 61