Home / Snippets / How to Access JSON Object Keys Having Spaces in JavaScript
How to Access JSON Object Keys Having Spaces in JavaScript cover

How to Access JSON Object Keys Having Spaces in JavaScript

1.9K

2 years ago

0 comments

Access JSON Object with Bracket Notation


The easiest way to access JSON Object Keys is to use bracket notation (using square bracket) and it's as follows.
var tom = {
    "id": "111",
    "No. of siblings": "5"
}

console.log(tom["No. of siblings"]);

Accessing Nested Array with Bracket Notation


The same goes for accessing nested arrays, you can use the array index and then refer to the array notation to get the empty spaces.
var toms = [
{
    "id": "111",
    "No. of siblings": "5"
},
{
    "id": "112",
    "No. of siblings": "3"
},
];

console.log(toms[0]["No. of siblings"]);
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this