3

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'];?

user3607612
  • 131
  • 1
  • 11
  • 2
    Using `obj['key-with-hyphens']` – Rayon Oct 05 '15 at 06:24
  • 1
    Possible duplicate of [Unable to access JSON property with "-" dash](https://stackoverflow.com/questions/13869627/unable-to-access-json-property-with-dash) – JJJ Jun 05 '17 at 08:19

2 Answers2

8

You can access it with this notation:

var val = obj["key-with-hyphens"];
slomek
  • 4,873
  • 3
  • 17
  • 16
4
> var obj = {"key-with-hyphens": "value"};
> obj["key-with-hyphens"];
< "value"
Mico
  • 1,978
  • 2
  • 13
  • 17