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

7

49

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?

Sennar answered 20/10, 2013 at 12:30 Comment(1)
Someone upvoted my answer and I wanted to remember what it was about, so I clicked through. Saw the snark in the answer. Yikes. There was just no call for that. I've removed the snark, and I apologize for it in retrospect. Happy coding!Superorganic
S
167

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.

Superorganic answered 20/10, 2013 at 12:34 Comment(6)
@TJCrowder Isn't that a slightly strange default to have?Sennar
@hitautodestruct: It is to me, but then, I'm not a scientist. :-)Superorganic
Kelvin (en.wikipedia.org/wiki/Kelvin) is the temperature unit from the "International System of Units". It is absolute, based on physics. It's zero is the "absolute zero". It looks as a quite natural choice for a "default", to me...Dree
@MarcoS: Sure, but this is weather information. 99.999999% of people consuming weather information are going to use Celsius or Fahrenheit, even (I'm willing to bet) the vast majority of meteorologists. Defaults are for the common case, not the one in a million case. :-)Superorganic
:-) I think it's some sort of "internal format"... That way user is "forced" to choose the "common" unit she prefers...Dree
There goes my hero. Watch him as he goes. Didn't even see the &units doc.Wenona
F
14

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

Foreignborn answered 20/10, 2013 at 12:36 Comment(4)
Thanks for taking the time to answer!Sennar
@Foreignborn It's showing error like this : {"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}Thiol
if you pass along with &lat=...&lng=...&units=metric not works anymore.Godbey
@Foreignborn try this example syntax:- api.openweathermap.org/data/2.5/… Example:- https://api.openweathermap.org/data/2.5/weather?lat=17.387140&lon=78.491684&appid=7d71c8xxxxxxxxxxxxx1f3693e96&mode=json&units=metricDeepdyed
C
5

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.

Cal answered 5/10, 2016 at 19:42 Comment(0)
I
1

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>
Impi answered 15/11, 2017 at 15:1 Comment(0)
S
1

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.

Simon answered 2/10, 2020 at 13:16 Comment(0)
S
1

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'
Sams answered 26/10, 2021 at 18:0 Comment(0)
J
0

Or you can create a simple function like that with one parameter ! (for celsius)

export function transformTemperature(data) {
    let temperature = data;
    let celsius = temperature - 273;
    let roundedTemp = Math.round(celsius)

    return roundedTemp
}
Justitia answered 26/1 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.