'How do I calculate the temperature in celsius returned in openweathermap.org JSON?
I'm getting the weather for a city using openweathermap.org.
The jsonp call is working and everything is fine but the resulting object contains the temperature in an unknown unit:
{
//...
"main": {
"temp": 290.38, // What unit of measurement is this?
"pressure": 1005,
"humidity": 72,
"temp_min": 289.25,
"temp_max": 291.85
},
//...
}
Here is a demo that console.log's the full object.
I don't think the resulting temperature is in fahrenheit because converting 290.38 fahrenheit to celsius is 143.544.
Does anyone know what temperature unit openweathermap is returning?
Solution 1:[1]
It looks like kelvin. Converting kelvin to celsius is easy: Just subtract 273.15.
Looking at the API documentation, if you add &units=metric to your request, you'll get back celsius.
Solution 2:[2]
That appears to be kelvin, but you can specify the format you want returned for the temp, e.g.:
http://api.openweathermap.org/data/2.5/weather?q=London&mode=json&units=metric
or
http://api.openweathermap.org/data/2.5/weather?q=London&mode=json&units=imperial
Solution 3:[3]
Kelvin to Fahrenheit is:
(( kelvinValue - 273.15) * 9/5) + 32
I've noticed not all of the OpenWeatherApp calls read the units parameter if its passed in. (An example of this error: http://api.openweathermap.org/data/2.5/group?units=Imperial&id=5375480,4737316,4164138,5099133,4666102,5391811,5809844,5016108,4400860,4957280&appid=XXXXXX) Kelvin is still returned.
Solution 4:[4]
You can change the unit to metric.
This is my code.
<head>
<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>
<style type="text/css">]
body{
font-size: 100px;
}
#weatherLocation{
font-size: 40px;
}
</style>
</head>
<body>
<div id="weatherLocation">Click for weather</div>
<div id="location"><input type="text" name="location"></div>
<div class="showHumidity"></div>
<div class="showTemp"></div>
<script type="text/javascript">
$(document).ready(function() {
$('#weatherLocation').click(function() {
var city = $('input:text').val();
let request = new XMLHttpRequest();
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=[YOUR API KEY HERE]`;
request.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
let response = JSON.parse(this.responseText);
getElements(response);
}
}
request.open("GET", url, true);
request.send();
getElements = function(response) {
$('.showHumidity').text(`The humidity in ${city} is ${response.main.humidity}%`);
$('.showTemp').text(`The temperature in Celcius is ${response.main.temp} degrees.`);
}
});
});
</script>
</body>
Solution 5:[5]
First Determine which Format do you want. Add Only &mode=json&units=metric after you send city in your BASE_URL. You will get dirrect Celsius value from the server.
Solution 6:[6]
Try this example
curl --location --request GET 'http://api.openweathermap.org/data/2.5/weather?q=Manaus,br&APPID=your_api_key&lang=PT&units=metric'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | spacebean |
| Solution 3 | Petter Friberg |
| Solution 4 | Darragh Blake |
| Solution 5 | Joyanta sarkar |
| Solution 6 | Ramon Costa |
