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.

1
  • 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. Commented May 16, 2020 at 16:00

4 Answers 4

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>

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

Comments

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.

1 Comment

Ah, I did not know how to do this using request
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.

Comments

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.

Comments

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.