Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've tried 100 different things, and spend days looking through Google and Stackoverflow, but I can't find a solution to this problem. Everything I call after the body of this API response returns undefined!

The response from Facebook SDK looks like this:

[
    {
        "body": "[   
            "data": [
                {
                    "name": "Larry Syid Wright",
                    "administrator": false,
                    "id": "xxx"
                },      {         
                    "name": "Melissa Long Jackson",
                    "administrator": false,
                    "id": "xxx"
                },      {       
                    "name": "Charlotte Masson",
                    "administrator": false,
                    "id": "xxx"  
                }  
            ],   
            "paging": {      
                "next": "url"
            }
        ]"
    },{
        "body": "{   
            "data": [      
                {         
                    "id": "xxx_xxx",       
                    "message": "In honor of Halloween, how many of you have your own ghost stories?  Who believes in ghosts and who doesn't?",                 
                    "type": "status",         
                    "created_time": "2014-10-31T20:02:01+0000",         
                    "updated_time": "2014-11-01T02:52:51+0000",         
                    "likes": {            
                        "data": [               
                            {                  
                                "id": "xxx",                  
                                "name": "Joe HerBatman Owenby Jr." 
                            }          
                        ],            
                    }
                    "paging": {               
                        "cursors": 
                            {                  
                                "after": "xxx",                  
                                "before": "xxx"               
                            }            
                        }         
                    }
                },{         
                    "id": "xxx_xxx",         
                    "from": {            
                        "id": "xxx",            
                        "name": "Jessica Starling"         
                    },       
                    "message": "Watching the "Campaign" and I can't help but notice what a fantastic job they did (Will ferrell and all) with that North Carolina accent! Ya'll know we sound different than other southern states ;)",         
                    "type": "status",           
                    "created_time": "2014-11-01T02:36:21+0000",         
                    "updated_time": "2014-11-01T02:36:21+0000",         
                    "likes": {            
                        "data": [               
                            {                  
                                "id": "xxx",                  
                                "name": "Scott Williams"n               
                            }         
                        ]
                    }      
                }      
            ],   
            "paging": {      
                "previous": "xxx",      
                "next": "xxx"  
            }
        }"
    }
]

This response is from a batch call. If I call them separately, I can easily iterate through the responses, and get everything from them. When I call them in the batch though, I can't get past "body", and I need to use a batch call.

console.log(response[0].body); will return the object inside the body of the first part of the response, but console.log(response[0].body.data); returns undefined. I just don't get it. This should be simple but it's like there's a lock on the door and I don't have the right key.

I normally have no issue iterating through objects, so I don't need a generalized answer. I need help seeing whatever it is here that I don't see. Why does the console show undefined when I call anything after the body, and what do I need to be doing to get any of these values?

share|improve this question
1  
These body values are just giant strings - essentially embedded JSON. You'd have to separate parse them as JSON to get at their internal values. –  jfriend00 Nov 1 '14 at 3:50

2 Answers 2

up vote 2 down vote accepted

That JSON contains nested JSON. body seems to be a string. Use

var body = JSON.parse(response[0].body);
share|improve this answer
    
Thank you so much! I didn't even know a response would be a string. What's the point of that? –  Justin White Nov 1 '14 at 4:04
1  
Check out the following article, it will totally answer your question: benalman.com/news/2010/03/theres-no-such-thing-as-a-json –  istos Nov 1 '14 at 4:07
    
@istos that definitely explains things, and makes a lot of sense. –  Justin White Nov 1 '14 at 4:31
    
Strings kind of makes sense, considering responses are strings, you have an array of responses (think of ajax responses having a responseText property) and their bodies are strings. Was this a call that combined multiple endpoints? @istos I am not sure how that article applies here, nobody was calling the object JSON –  Juan Mendes Nov 1 '14 at 4:35
    
@Juan Mendes, The OP found it helpful and the article totally relates, you just have to read the whole article and not just the title. –  istos Nov 1 '14 at 5:00

The values from the body are just strings.which are embedded as json.So firstly you would need to parse them using JSON.parse.

The code would be like

var body = JSON.parse(response[0].body);
share|improve this answer
    
Thanks! Someone answered ahead of you with the same thing, so I marked that one as the answer. I got what I needed though so thank you! –  Justin White Nov 1 '14 at 4:04
1  
it fine ...:) ..Hapy coding –  Avinash Babu Nov 1 '14 at 4:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.