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?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
hitautodestruct
  • 20,081
  • 13
  • 69
  • 93
  • 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! – T.J. Crowder Jul 25 '21 at 14:34

6 Answers6

163

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @TJCrowder Isn't that a slightly strange default to have? – hitautodestruct Oct 20 '13 at 12:46
  • 4
    @hitautodestruct: It is to *me*, but then, I'm not a scientist. :-) – T.J. Crowder Oct 20 '13 at 12:53
  • 4
    Kelvin (http://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... – MarcoS Mar 03 '14 at 13:02
  • 1
    @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. :-) – T.J. Crowder Mar 03 '14 at 13:18
  • :-) I think it's some sort of "internal format"... That way user is "forced" to choose the "common" unit she prefers... – MarcoS Mar 03 '14 at 13:41
  • 2
    There goes my hero. Watch him as he goes. Didn't even see the &units doc. – ReSpawN Oct 02 '15 at 06:32
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

spacebean
  • 1,554
  • 1
  • 10
  • 13
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.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Sara
  • 93
  • 1
  • 2
  • 6
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>
Darragh Blake
  • 238
  • 1
  • 6
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.

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'