0

This is my json:

{

    "xuid":****************,
    "state":"Online",
    "devices":[
        {
            "type":"XboxOne",
            "titles":[
                {
                    "id":750323071,
                    "name":"Accueil",
                    "placement":"Background",
                    "state":"Active",
                    "lastModified":"2018-02-23T17:09:38.4018406Z"
                },
                {
                    "id":1145574011,
                    "activity":{
                        "richPresence":"Dans un salon priv\u00e9"
                    },
                    "name":"Titanfall\u2122 2",
                    "placement":"Full",
                    "state":"Active",
                    "lastModified":"2018-02-23T17:09:38.4018406Z"
                }
            ]
        }
    ]

}

I would like to recover the value: devices->titles->1->activity->richPresence
This is my code with nodeJs:

function (error, response, body) {
    if (!error && response.statusCode === 200) {
        var xbJson = JSON.parse(body);          
        console.log(xbJson.devices.titles[1]['name'])
    }
}

My error:

TypeError: Cannot read property '1' of undefined
    at Request._callback (/home/atmoner/Bureau/xboxlive/test.js:16:40)
    at Request.self.callback (/home/atmoner/Bureau/xboxlive/node_modules/request/request.js:186:22)
    at emitTwo (events.js:125:13)
    at Request.emit (events.js:213:7)
    at Request.<anonymous> (/home/atmoner/Bureau/xboxlive/node_modules/request/request.js:1163:10)
    at emitOne (events.js:115:13)
    at Request.emit (events.js:210:7)
    at IncomingMessage.<anonymous> (/home/atmoner/Bureau/xboxlive/node_modules/request/request.js:1085:12)
    at Object.onceWrapper (events.js:312:19)
    at emitNone (events.js:110:20)

I guess the problem comes from the array in a json and I can not move further.
It may sound simple but I need some help. Thank you!...

1
  • Wouldn't it be console.log(xbJson.devices[0].titles[1]['name']) as devices is an array? Commented Feb 24, 2018 at 15:39

1 Answer 1

1

Try this out:

console.log(xbJson.devices[0].titles[1]['name'])
Sign up to request clarification or add additional context in comments.

4 Comments

TypeError: Cannot read property 'name' of undefined The problem comes after [1] When i try this, it work: console.log(xbJson.devices[0].titles) result: i.imgur.com/CVmX0Zc.jpg
devices is an array of length 1,and it contains one object i.e. accessible by devices[0].Instead of writing devices,you need to write devices[0].When you write devices.titles[1] it can't find it because devices is an array,not object.Thus titles remain undefined in your case.
@Julien your screenshot shows that the titles array only contains one element, which is different from the JSON provided in your post. You probably want titles[0], not titles[1] in that case. But I would also recommend not hard coding these values either if your titles array can potentially be multiple lengths.
@KhauriMcClain & Ayan My bad!! Indeed, my console was only on the main acch .... so he could not recover something that does not exist! By launching a game, I can recover the good value ... I will opt for a foreach and thus recover all the elements in a loop ... Thank you very much for your help !!

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.