2

What is the reason here while accessing JSON object using . gives error but using [] it works? what is the correct syntax to access the JSON object key?

This syntax gives the error:

var obj2G = obj.2G;

This syntax is working but why need to access this way?

var obj2G = obj["2G"];

var obj = {
  "2G": [{
    "essid": "SINGTEL-662F",
    "authmode": "psk psk2",
    "authkey": "0000026159",
    "isEnable": "1",
    "isHidden": "0",
    "hwaddr": "E0:8E:3C:00:66:30",
    "opmode": "ap"
  }, {
    "essid": "GUEST1-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "",
    "opmode": "ap"
  }, {
    "essid": "GUEST2-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "",
    "opmode": "ap"
  }, {
    "essid": "GUEST3-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "62:8E:3C:00:66:33",
    "opmode": "ap"
  }],
  "5G": [{
    "essid": "SINGTEL-662F(5G)",
    "authmode": "psk psk2",
    "authkey": "0000026159",
    "isEnable": "1",
    "isHidden": "0",
    "hwaddr": "E0:8E:3C:00:66:31",
    "opmode": "ap"
  }, {
    "essid": "GUEST1(5G)-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "6a:8e:3c:00:66:32",
    "opmode": "ap"
  }, {
    "essid": "GUEST2(5G)-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "",
    "opmode": "ap"
  }, {
    "essid": "GUEST3(5G)-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "6a:8e:3c:00:66:30",
    "opmode": "ap"
  }]
}

obj2G = obj["2G"];
console.log(obj2G);

obj2G = obj.2G;
console.log(obj2G);

1

3 Answers 3

2

If you use dot notation:

property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object.$1 is valid, while object.1 is not.

In that case, you have to use bracket notation:

property_name is a string. The string does not have to be a valid identifier; it can have any value, e.g. "1foo", "!bar!", or even " " (a space).

See the documentation: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors

It's worth mentioning that you have to use bracket notation if you want to access a property using a variable. For instance:

var myName = "foo";
var myObject = { foo: 42};
console.log(myObject.myName);//returns undefined

But:

var myName = "foo";
var myObject = { foo: 42};
console.log(myObject[myName]);//returns 42
Sign up to request clarification or add additional context in comments.

Comments

2

Object properties must start with a letter when used in dot notation. It's just a rule in Javascript. You can name property literally '*&^' but you have to do it via [] notation.

2G starts with a number thus requires [] notation.

3 Comments

i understood overall but can't understood what you wrote about '*&^' ?
I mean you can add a property named *&^ to an object like object['*&^'] = 6;
Found an article related to this
1

From the documentation to Property Accessor for dot notation:

In this code, property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object.$1 is valid, while object.1 is not.

And to Variable

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

The result is, a variable or a property in dot notation with starting number can not be used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.