1

I am trying to get JSON from API and then access "main" object of the "weather" object of the JSON.

When I use this code:

$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", function(json) {
  var data = JSON.stringify(json);
  alert(data);
});

I get this output:

{
  "coord": {
    "lon": 159,
    "lat": 35
  },
  "weather": [{
    "id": 500,
    "main": "Rain",
    "description": "light rain",
    "icon": "https://cdn.glitch.com/6e8889e5-7a72-48f0-a061-863548450de5%2F10n.png?1499366021399"
  }],
  "base": "stations",
  "main": {
    "temp": 22.59,
    "pressure": 1027.45,
    "humidity": 100,
    "temp_min": 22.59,
    "temp_max": 22.59,
    "sea_level": 1027.47,
    "grnd_level": 1027.45
  },
  "wind": {
    "speed": 8.12,
    "deg": 246.503
  },
  "rain": {
    "3h": 0.45
  },
  "clouds": {
    "all": 92
  },
  "dt": 1499521932,
  "sys": {
    "message": 0.0034,
    "sunrise": 1499451436,
    "sunset": 1499503246
  },
  "id": 0,
  "name": "",
  "cod": 200
}

Now, the output that I am trying to get is "Rain" (the property of the "main" object of the "weather" object (I hope I said this correctly, I'm a beginner)).

So logically, I would do this:

$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", function(json) {
  var data = JSON.stringify(json);
  alert(data["weather"].main);
});

But that doesn't give me any output.

I did some search, and found out that I should parse.

But when I did:

$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", function(json) {
  var data = JSON.stringify(json);
  var Jason = JSON.parse(data);
  alert(Jason["weather"].main);
});

I got undefined as my output again.

So, what should my code look like so my output would be "Rain"?

PS: Sorry if I made mistakes in describing my issue, I am really new to JavaScript/jQuery and also English is my second language.

6
  • Don't stringify the object if you want to access parts of the object. Commented Apr 10, 2018 at 17:13
  • could you try alert(json["weather"])? Commented Apr 10, 2018 at 17:14
  • Why don't you ask this in the FCC forum? Commented Apr 10, 2018 at 17:25
  • @JohnnyBizzle because it's not an FCC problem, but a misunderstanding of how arrays work, I guess Commented Apr 10, 2018 at 17:27
  • 1
    This forum is way more active/useful. Also, since this problem isn't related purely to FCC, but applies to general usage of jQuery, I don't see why I shouldn't ask this here. Commented Apr 11, 2018 at 11:48

2 Answers 2

3

You nearly have it, simply add [0] after accessing the weather. Since weather is an Array, you need this to get the data from the first element:

$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", 
    json => console.log(json.weather[0].main)
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Also, the getJSON function already parses the JSON for you, no need for additional JSON.parses

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

Comments

0

JSON.Stringify will convert json to string. If you want to access objects, you need JSON object and not string.

weather is an array of objects and you need to access an array using index. As you want 1st object, use json["weather"][0]

$.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139", function(json){
  alert(json["weather"][0].main);
});

4 Comments

Small question. Why the extra local variable?
Please, add some explanation in your answer.
@DannyFardyJhonstonBermúdez Just added. Thank you.
Actually there is no need of that variable.

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.