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.
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.
Separator. Double colon ( :: ) is used as a seperator for nested JSON arrays.
To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.
{} denote containers, [] denote arrays.
It's simple:
photo[':original'].id
Dot/bracket notation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With