Ask Your Question
4

What is the method for accessing a JSON key containing a hyphen?

asked 2021-04-09 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-02-19 04:00:00 +0000

qstack gravatar image

To access a JSON key containing a hyphen, you can use either dot notation or bracket notation. With dot notation, you simply replace the hyphen with an underscore:

myJsonKey.mySubKey // accessing with dot notation

The above example assumes that the JSON object has a key named "myJsonKey" and a sub-key named "my-sub-key". With dot notation, we replace the hyphen with an underscore:

myJsonKey.my_sub_key // accessing with dot notation

Alternatively, you can use bracket notation and include the key inside quotes:

myJsonKey["my-sub-key"] // accessing with bracket notation
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-09 11:00:00 +0000

Seen: 14 times

Last updated: Feb 19 '22