skip to Main Content

Ok I’m having a brain fart. I need to pull out the value for "data" as described below…

(UPDATED:) console.log(d.body) gives me...
 {
    "data": [{
        "id": "30220059",
        "login": "esl_sc2",
        "display_name": "ESL_SC2",
        "type": "",
        "broadcaster_type": "partner",
        "description": "For standings, schedule, and results, visit https://pro.eslgaming.com/tour/sc2/",
        "profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/cac110f7-0711-421f-9341-c77b53ab63b5-profile_image-300x300.jpeg",
        "offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/ac0e240d-63c2-4faf-9040-60543b3e2c0f-channel_offline_image-1920x1080.jpeg",
        "view_count": 0,
        "created_at": "2012-05-02T09:59:20Z"
    }]
}

What do I need to do on d.body to get the output to look like this?

{
    "id": "30220059",
    "login": "esl_sc2",
    "display_name": "ESL_SC2",
    "type": "",
    "broadcaster_type": "partner",
    "description": "For standings, schedule, and results, visit https://pro.eslgaming.com/tour/sc2/",
    "profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/cac110f7-0711-421f-9341-c77b53ab63b5-profile_image-300x300.jpeg",
    "offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/ac0e240d-63c2-4faf-9040-60543b3e2c0f-channel_offline_image-1920x1080.jpeg",
    "view_count": 0,
    "created_at": "2012-05-02T09:59:20Z"
}

I thought it was simply d.body.data[0]?

2

Answers


  1. Chosen as BEST ANSWER

    Turns out typeof d.body is my friend here. It was returning a string not an object.


  2. you can get direct the json using the following Destructuring assignment

    Destructuring assignment

    let { data: [data = {}] = [0] } = {
        "data":[
            {
                "id":"30220059",
                "login":"esl_sc2",
                "display_name":"ESL_SC2",
                "type":"",
                "broadcaster_type":"partner",
                "description":"For standings, schedule, and results, visit https://pro.eslgaming.com/tour/sc2/",
                "profile_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/cac110f7-0711-421f-9341-c77b53ab63b5-profile_image-300x300.jpeg",
                "offline_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/ac0e240d-63c2-4faf-9040-60543b3e2c0f-channel_offline_image-1920x1080.jpeg",
                "view_count":0,
                "created_at":"2012-05-02T09:59:20Z"
            }
        ]
    }
    
    console.log(data)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search