0

I am trying to use openweathermap.org weather api, I am getting the data but having trouble parsing it.

Here's my code:

request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test", function(error, response, body) {
    if (!error && response.statusCode == 200) {
        var parsedData = JSON.parse(body);
        console.log(typeof body);
    }
});

console.log(typeof body); returns string so I just can't figure out what 's the problem.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Deepak singh
  • 35
  • 1
  • 4
  • That API is a JSONP API, and it does not respond with parseable JSON. Instead, the response takes the form of a JavaScript function call. That's what the "callback" parameter is for. – Pointy May 16 '20 at 16:00

4 Answers4

1

You are looking for this - the URL is JSONP so it expects a function called test and no need to parse

<script>
const test = data => console.log(data);
</script>
<script src="https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test"></script>

Alternatively remove the callback - here using fetch:

fetch("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric")
  .then(response => response.json())
  .then(data => console.log(data));

Or Axios

axios.get('https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric')
  .then(response => console.log(response.data));
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

This worked for me. Just set object for the first argument with url and json property. set json:true. Setting to true will handle the parsing for you :)

const request = require('request');
const url = "https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric";

request({ url: url, json: true }, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});

As @mplungjan pointed out. That's a JSONP url.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Samuel
  • 5,529
  • 5
  • 25
  • 39
1

Remove &callback=test at the end of your URL then you don't have to deal with JSONP and you can work with it like normal.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
0

Since you are using an API which has a callback function return as response. It is not JSON but JSONP (JSON with Padding). Your code will work just fine if you remove the callback parameter from URL.

request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric", function(error, response, body) {
       if (!error && response.statusCode == 200) {
          console.log(response);
       }
   });

For further reading about JSONP, You can refer this.

Rush W.
  • 1,321
  • 2
  • 11
  • 19