Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a JSON object with a colon in the key

I'm using a third party tool that POSTs a JSON response. It works great, but one of the keys I need to use has a colon in it and I have no idea how to select this object in JavaScript.

For example:

{   "photo": {     "reg": {       "id": 50     },     "thumb": {       "id": 51     },     ":original": {       "id": 53"     }   } } 

How do I select photo.:original.id? I get syntax errors when I leave the colon in, and undefined when I try dropping the colon.

like image 959
Eric Koslow Avatar asked Feb 07 '11 19:02

Eric Koslow


People also ask

Is colon allowed in JSON?

JSON name-value pairs exampleName-value pairs have a colon between them as in "name" : "value" . JSON names are on the left side of the colon. They need to be wrapped in double quotation marks, as in “name” and can be any valid string. Within each object, keys need to be unique.

What does colon mean at JSON?

Separator. Double colon ( :: ) is used as a seperator for nested JSON arrays.

How do I access a specific JSON object?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

What is the difference between {} and [] in JSON?

{} denote containers, [] denote arrays.


1 Answers

It's simple:

photo[':original'].id 

Dot/bracket notation

like image 163
kirilloid Avatar answered Sep 27 '22 20:09

kirilloid