How to get info after using JSON.stringify() with an API?

How to get info after using JSON.stringify() with an API?
0

#1

Hello,
I used the JSON.stringify() method, as shown in the FCC lessons, to get the structure of the APIs that I am currently working with for the current weather project. The problem is that I don’t know how to get the data that I want out of the result of the stringify method. Is there any standard method that traverses the output of stringify that can grab out data based on a specific key/ value ? If there is another, better way, please feel free to mention that too. Any specific examples would be greatly appreciated!
Thanks!


#3

If I had something like this:

$.getJSON(“http://ip-api.com/json”, function(json) {

$(".message").html(JSON.stringify(json));
});

Do I need to create a variable outside of the getJSON function and set it equal to the stringify output? I tried doing it inside the function, and it didn’t seem to work. Could you show me how you would approach this in this situation?
Thanks!


#4

I think I fixed that problem, but now here is the problem I am dealing with:

$.getJSON(“http://ip-api.com/json”, function(json) {
var result = JSON.stringify(json);
var lat = result[“lat”];
console.log(lat);
console.log(result);
$(".message").html(JSON.stringify(json));
});

Console.log(lat) returns undefined. Why is this happening? Did I set up the lat variable incorrectly? My goal is to get the number value for the lat assigned to the lat variable.

Thanks!


#6

Thanks! You are a life saver!


#7

In order for the open weather API that I’m using to access the data I grabbed from the geolocation API, do I need to nest the open weather api getJSON function inside the getJSON for the geolocation? The variable scope seems to be limited to each getJSON function.