Access to json is returning error

3

I have the following json:

x = {
    "body-json": {
        "id": "1"
    }
}

I'm trying to access the id this way:

console.log(x.body-json.id);

It turns out that I can not access the reference body-json . Can anyone help me?

    
asked by anonymous 01.07.2016 / 19:54

1 answer

5

You need to use the second syntax to access your object:

console.log(x["body-json"].id)

Since the expression x.body-json.id is interpreted as x.body - json.id according to the JavaScript syntax rules. To access the variable you want, you need to use the syntax that passes the property name as a string.

    
01.07.2016 / 19:57