Posts Learn Components Snippets Categories Tags Tools About
/

How to Access JSON Object Keys Having Spaces in JavaScript

Learn how to access JSON Object keys having spaces in JavaScript the easy way

Created on May 23, 2022

1866 views

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"]);

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

)