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!

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
running JSON.stringify(response); gave me TypeError: Converting Circular Structure to JSON
About working with circular structures read here: stackoverflow.com/questions/10392293/…
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.

1 Comment

Stringify gave me an error ... "TypeError: Converting circular structure to JSON".. But doing it as console.log(response.status); returns undefined
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);

6 Comments

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
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.
Yeah, I tried that too but no luck. Thanks for trying to help! I appreciate it!
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.
Working with circular objects: stackoverflow.com/questions/10392293/…
|

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.