0

I'm not quite sure what my problem is here.

This is my json:

user = {
    userdata: {
        name: "Test"
    }
}

And while user.userdata.name returns Test, user["userdata.name"] returns undefined.

Also user["userdata"] is returning the userdata json just as well. And user["userdata"]["name"] is also returning Test.

I'm using the same method on another json, but not searching to deep. just for userdata. and there is works just fine...

2
  • 1
    "This is my json" No, it isn't. It's a JavaScript object initializer being assigned to a variable. JSON is a textual notation for data exchange (and doesn't have the = operator, because it's not a programming language). If you're in JavaScript source code, and not talking about the contents of a string, it's not JSON. Commented Sep 25, 2015 at 12:04
  • 1
    What about user["userdata"]["name"] . Commented Sep 25, 2015 at 12:06

2 Answers 2

4

And while user.userdata.name returns Test, user["userdata.name"] returns undefined.

Correct. The bit in quotes is used, in its entirety, as the property name to look up. It's not parsed. Since your user object doesn't have a property called userdata.name (it has userdata, which in turn has name), the value you get is undefined.

If you wanted to access that name with brackets notation it would be user["userdata"]["name"] (where the strings can be literal strings, as shown, or the result of any expression such as a variable lookup, string concatenation, etc.).

Sign up to request clarification or add additional context in comments.

Comments

2

Can Access like this.

user["userdata"]["name"]

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.