0

So, I'm working on getting info from an API through Node.js the returned file from the API server is a JSON file and I've tried parsing it through JSON.parse(); but to no avail. I've tried calling the api through https.request(); , http.get(); , and also using request(url, function); and I am able to put the returned value into process.stdout.write(returnedJson); .. But I can't get it to the point where I could loop through the values, or even pick out a specific value.

Here is an example of the JSON I get from the api ...

{
    status: 0,
    data: [
       {
       TripId: 12442,
       Name: John Doe, 
       Date: April 1, 1970
       }, 

       {
       TripId: 35314,
       Name: Jane Doe,
       Date: April 2, 1970
       }

    ]
}

When I used JSON.parse(returnedJson); I usually get [object Object] as my return from that if I try to use console.log(returnedJson);

Any Help would be great! If I need to add some more info please let me know and I will!

AJ Fick
  • 139
  • 1
  • 16

3 Answers3

1

What you receive is an object as it logs. If you want to log it properly do console.log(JSON.stringify(response, null, " "));

(that empty space between quotes is 4 spaces - just for nicelooking print).

Paweł Smołka
  • 638
  • 5
  • 13
  • The 'console.log'-info was more for testing.. Will stringify allow me to cycle through the data to add it to a DB or something like that? – AJ Fick Jun 21 '16 at 00:18
  • JSON.stringify(response) is enough for you to get correct string from JSON format. However this is not an object after that. So to work on this object you have to use your normal response object. Then u use it like response.status is equal to 0 in your example, response.data[0] is that first object in array and so on. – Paweł Smołka Jun 21 '16 at 00:25
  • running `JSON.stringify(response);` gave me `TypeError: Converting Circular Structure to JSON` – AJ Fick Jun 21 '16 at 01:27
  • About working with circular structures read here: http://stackoverflow.com/questions/10392293/stringify-convert-to-json-a-javascript-object-with-circular-reference – Paweł Smołka Jun 21 '16 at 07:24
0

First I'd check to see if your response has valid data.
You could do something like: str = JSON.stringify( response ); console.log( str ); If it's valid, then just use the response: console.log( response.status ); console.log( response.data[0] ); etc...

[object object] usually means that you have a javascript object already. No need to parse it.

Aaron
  • 428
  • 5
  • 14
  • Stringify gave me an error ... "TypeError: Converting circular structure to JSON".. But doing it as `console.log(response.status);` returns `undefined` – AJ Fick Jun 21 '16 at 01:24
0

Depending on the framework you are using, you are either getting a String or an Object out. (It looks like you're getting an Object). To check what you are getting, use:

console.log(typeof(response));

If it's a String, use:

response = JSON.parse(response);

to turn it into an Object.

Once you have an Object, you can access the properties using "dot" notation etc. like any other javascript object:

Such as:

console.log(response.status);
console.log(response.data[0].TripId);
console.log(response.data[1].TripId);
dmansfield
  • 1,108
  • 10
  • 22
  • I ran `console.log(typeof(response));` and got `object` , then when I tried `console.log(response.data[0]);` I get an error.. `TypeError: Cannot read property '0' of undefined` – AJ Fick Jun 21 '16 at 01:20
  • Then I recommend you do (as the other answer recommends) `console.log(JSON.stringify(response, null, " "))`. That will shouw you the structure of your object. Something isn't right but I don't understand what with the limited information. – dmansfield Jun 21 '16 at 01:22
  • Yeah, I tried that too but no luck. Thanks for trying to help! I appreciate it! – AJ Fick Jun 21 '16 at 01:32
  • You mention "returnedJson" variable above, but then below you use "response". Do you really have two variables? You should be using the "returnedJson" variable not "response" I think. – dmansfield Jun 21 '16 at 02:53
  • Working with circular objects: http://stackoverflow.com/questions/10392293/stringify-convert-to-json-a-javascript-object-with-circular-reference – Paweł Smołka Jun 21 '16 at 07:25
  • @dmansfield You're right, That was a slip up on my part. It is the same thing I just misnamed it. – AJ Fick Jun 22 '16 at 14:19