2

I have another simple question about Node. I'm trying to make a simple http request to the Open Courseware search API (http://www.ocwsearch.com/api), but I'm running into some problems actually parsing the JSON... which normally hasn't been a problem. The request returns a string that has escaped characters and slashes, and so I've run a replace and an unescape on the response string, but this ends up returning something like '[object Object]'. Right now, all I really want to do is be able to SEE what's finally being returned, so I can tell whether or not I can finally parse it as valid JSON. Unfortunately, this is not working either. I've read a couple of similar threads on stack overflow, but I'm still unable to get it to work.

What I've tried:

  • iterating over the object being passed, because I thought it'd be something with key value pairs, and running over it, logging it each time. However, this prints it out as a string:

[

o

b

... all the way to the end (])

Given x as the returned formatted object in question,

None of these seem to work though, they're all returning me [object Object].

I've even tried it in jsfiddle: http://jsfiddle.net/S47QL/2/ I'm replacing console.log with alert, and it seems to be re

My code:

var request = require('request');$
request('http://www.ocwsearch.com/api/v1/search.json?q=' + skill +     '&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/',$
  function(error, response, body){$
  if(!error && response.statusCode == 200){~$
      console.log(response.toString().replace(/\\\//g, "/"));$
        var x = response.toString().replace(/\\\//g, "/");$
        console.log(x)$
        console.log(x.keys());$
  }$  
});$
Community
  • 1
  • 1
dxu
  • 459
  • 2
  • 5
  • 13

1 Answers1

1

Right now, all I really want to do is be able to SEE what's finally being returned, so I can tell whether or not I can finally parse it as valid JSON.

So then make the request in a browser window (using Chrome you can see the actual results being returned in the inspector tabs - press F12).

Example: http://www.ocwsearch.com/api/v1/search.json?q=ios&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/

And according to http://jsonlint.com/ this is valid JSON being returned. Are you sure you're not trying to be too smart for your own good?

You can't write the response to string, because it's an object. You need the data returned, but I think the body field does what you want. Have you tried just printing body?

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • you could also loop over the keys in body, and see what that prints out (`for (var k in body){ console.log(k) }`) – jcolebrand Jun 16 '12 at 09:02
  • Ack, I was being silly. Working late in the night I sometime replaced "body" with "response"... I probably should have been more clear, when I try to print the json being returned, it gets printed with a large amount of escape slashes on the server (it was escaping the original escape sequences. For example, it was returning "http:\\/\\/..."). I was trying to remove them because it wasn't allowing me to just call the attributes directly, even though I put them directly into the browser and it looked fine. Also, above I posted that I was unable to just loop over it, although I think that this – dxu Jun 16 '12 at 18:02
  • was just me being tired at night and not realizing that I somewhere in between changed the "body" variable to "response", which wasn't returning me anything... – dxu Jun 16 '12 at 18:02
  • *wasn't returning me what I wanted to see And yup... that was the problem.. all I had to do was run everything on body instead, and then run JSON.parse() on it. Stupid of me... Thanks! – dxu Jun 16 '12 at 18:09
  • Glad you got it sorted out :D – jcolebrand Jun 16 '12 at 21:22