user3607612
user3607612

Reputation: 131

How To Access JSON Keys With Hyphen

Normally, we access json object elements using dot notation i.e. var obj = {"key": "value"}; var val = obj.key;. How do we access the value in case of var obj = {"key-with-hyphens": "value"};? Do I have to revert to [] i.e. var val = obj['key-with-hyphens'];?

Upvotes: 3

Views: 6620

Answers (2)

Mico
Mico

Reputation: 2009

> var obj = {"key-with-hyphens": "value"};
> obj["key-with-hyphens"];
< "value"

Upvotes: 4

slomek
slomek

Reputation: 5126

You can access it with this notation:

var val = obj["key-with-hyphens"];

Upvotes: 9

Related Questions