Archie Ec
Archie Ec

Reputation: 70

Retrieving data from a JSON sub array in javascript, where identifier starts with an integer

I must be missing something simple here, but I'm having trouble retrieving data from a JSON array response. I can access objects with identifiers that start with letters, but not ones that start with numbers.

For example, I can access

data.item[0].specs.overview.details

But I can't access

data.item[0].specs.9a99.details

Upvotes: 3

Views: 1566

Answers (5)

amelvin
amelvin

Reputation: 9051

Javascript doesn't like variables or identifiers that start with a number, this reference states that only:

Any variable name has to start with
_ (underscore) 
$ (currency sign) 
a letter from [a-z][A-Z] range 
Unicode letter in the form \uAABB (where AA and BB are hex values)

are valid first characters.

Upvotes: 2

heisthedon
heisthedon

Reputation: 3707

A variable name in javascript cannot start with a numeral. That's the reason why it doesn't work.

Upvotes: 2

timdev
timdev

Reputation: 62894

Use bracket notation

that is:

data.item[0].specs["9a99"].details

Upvotes: 5

Mahesh Velaga
Mahesh Velaga

Reputation: 21971

Try this,

data.items[0].specs["9a99"].details

Upvotes: 2

Gumbo
Gumbo

Reputation: 655319

Identifier literals must not begin with a number because they would be confused with number literals. You need to use the bracket syntax in this case:

 data.item[0].specs["9a99"].details

Upvotes: 5

Related Questions