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