Extract temperature from JSON

Hello everyone,

I need to extract one value from two different Web Dat API in JSON:

First one:

{"current":{"ts":"2018-09 19T10:56:59.869Z","tp":25.8,"hm":53,"p2":11,"p1":17,"co":575}, "historical":{"instant":[{"ts":"2018-09-19T10:56:59.869Z","tp":25.8,"hm":53,"p2":11,"p1":17,"co":575},{"ts":"2018-09-19T10:55:59.882Z","tp":25.8,"hm":53,"p2":10,"p1":11,"co":575},{"ts":"2018-09-19T10:55:00.537Z","tp":25.7,"hm":53,"p2":12,"p1":21,"co":575},{"ts":"2018-09-19T10:53:59.865Z","tp":25.8,"hm":53,"p2":11,"p1":15,"co":583}, ecc

Second one:

{"cod":"200","message":0.0047,"cnt":40,"list":[{"dt":1537358400,"main":{"temp":290.52,"temp_min":290.52,"temp_max":291.333,"pressure":974.52,"sea_level":1027.16,"grnd_level":974.52,"humidity":95,"temp_kf":-0.81},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":92},"wind":{"speed":1.36,"deg":339},"rain":{"3h":0.065},"sys":{"pod":"d"},"dt_txt":"2018-09-19 12:00:00"},{"dt":1537369200,"main":{"temp":291.76,"temp_min":291.76,"temp_max":292.365,"pressure":975.68,"sea_level":1028.4,"grnd_level":975.68,"humidity":84,"temp_kf":-0.61},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":88},"wind":{"speed":1.76,"deg":2.00037},"rain":{"3h":0.0575},"sys":{"pod":"d"},"dt_txt":"2018-09-19 15:00:00"},{"dt":1537380000,"main":{"temp":293.26,"temp_min":293.26,"temp_max":293.671,"pressure":976,"sea_level":1028.52,"grnd_level":976,"humidity":75,"temp_kf":-0.41},ecc

I need only the current temp that I generate with the Fetch on the web Data and I would like with a Dat Execute to update a Table only with the current temperature.
I try to understand and code all the suggestion that I find online and on this forum but I don’t understand where it’s my mistake or errors (newbie with this kind of operation).

I leave here my code for the first one:

[code]import json

Temp = op(‘jsonObj’)
def tableChange(dat):
Temp.clear()
stringVar = ‘{“tp”:“value”}’
jsonObj = json.loads(stringVar)
Temp.appendRow(jsonObj[‘tp’])[/code]

Hope some one of you can help me out :slight_smile:
Thank you in advance

Here’s an example of what you’re trying to do. This scene uses a web dat to fetch json data of the current ISS position from api.open-notify.org.

You shouldn’t need to clear the table each time you write to it. You can write to specific table cells using table[row,col]

The json received from the server looks something like this:

{"message": "success", "iss_position": {"longitude": "-83.2282", "latitude": "10.0507"}, "timestamp": 1537401288}

Attached to the web dat is a dat execute written like this:

[code]import json

def onTableChange(dat):

# load the json data
data = json.loads(dat.text)

# get the message
message = data['message']

#get the timestamp
timestamp = data['timestamp']

# the iss_position item has 2 children: longidude, latitude
iss_position = data['iss_position']
longitude = iss_position['longitude']
latitude = iss_position['latitude']

# store the data to a table
data_table = op('dataTable')
data_table['message', 1] = message
data_table['longitude', 1] = longitude
data_table['latitude', 1] = latitude
data_table['timestamp', 1] = timestamp
return[/code]

Hope this helps!
iss_json_example.toe (4.13 KB)

Hello Matthew,

thank you so much for your help, I past the few days to try the different API.
I had no problems with the first one but with the second one I didn’t manage to extract the temperature, I will continue to try :slight_smile:

Btw it’s appear this sign with this warning in TD, do you know how to solve it?
(I’m using TD on a Mac for the installation)

Thank you!

Hello again,

I write you here the JSON that I use now

{"status":"success", "data":{"city":"Berlin","state":"Berlin","country":"Germany","location":{"type":"Point","coordinates":[13.31825,52.463611]}, "current":{"weather":{"ts":"2018-09-25T06:00:00.000Z","hu":93,"ic":"01d","pr":1034,"tp":5,"wd":280,"ws":3.6}, "pollution":{"ts":"2018-09-25T06:00:00.000Z","aqius":33,"mainus":"p2","aqicn":16,"maincn":"n2"}}}}

And here my code

[code]
import json

def onTableChange(dat):

# load the json data
data = json.loads( dat.text)


status = data ['status']

current = data['current']
weather = data['weather']
ts = weather['ts']
hu = weather['hu']
ic = weather['ic']
pr = weather['pr']
tp = weather['tp']
wd = weather['wd']
ws = weather['ws']


data_table = op('dataTable3')

data_table['ts', 1] = ts
data_table['hu', 1] = hu
data_table['ic', 1] = ic
data_table['pr', 1] = pr
data_table['tp', 1] = tp
data_table['wd', 1] = wd
data_table['ws', 1] = ws
data_table['status', 1] = status
return[/code]

Where is my mistake to extract the “weather” data?

Thank you,
Andrea

Hello everyone,

I solved in this way (maybe is helpful for someone / a stupid thing)

[code]import json

def onTableChange(dat):

data = json.loads( dat.text)

status = data ['status']

current = data['data']['current']
weather = current['weather']
ts = weather['ts']
hu = weather['hu']
ic = weather['ic']
pr = weather['pr']
tp = weather['tp']
wd = weather['wd']
ws = weather['ws']

# store the data to a table
data_table = op('dataTable3')

data_table['ts', 1] = ts
data_table['hu', 1] = hu
data_table['ic', 1] = ic
data_table['pr', 1] = pr
data_table['tp', 1] = tp
data_table['wd', 1] = wd
data_table['ws', 1] = ws
data_table['status', 1] = status
return[/code]

I still have the warning on the DAT Excute,
no idea why

Thank you

Unfortunately the image you sent is a bit too low-res for me to see what the error says but I think the issue is in this line:

status = data ['status']

There shouldn’t be a space after ‘data’

status = data['status']

Also I thought I’d mention that there’s a cool python module called pprint that beautifies dictionaries (and other things) to make them more human readable:

{'data': {'city': 'Berlin', 'country': 'Germany', 'current': {'pollution': {'aqicn': 16, 'aqius': 33, 'maincn': 'n2', 'mainus': 'p2', 'ts': '2018-09-25T06:00:00.000Z'}, 'weather': {'hu': 93, 'ic': '01d', 'pr': 1034, 'tp': 5, 'ts': '2018-09-25T06:00:00.000Z', 'wd': 280, 'ws': 3.6}}, 'location': {'coordinates': [13.31825, 52.463611], 'type': 'Point'}, 'state': 'Berlin'}, 'status': 'success'}

you can use pprint like so:

[code]from pprint import pprint

pprint(some_dict)[/code]