How do I calculate the temperature in celsius returned in openweathermap.org JSON?

Solution 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:

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:

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:

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:

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.