Json class dict store in variables

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
mahnonsaprei
Posts: 5
Joined: Sun Sep 03, 2017 1:43 pm

Json class dict store in variables

Postby mahnonsaprei » Mon Sep 11, 2017 10:26 pm

Hi there,
my name's Marcello from italy and i'm a newbie of python. I try to make a weather station with esp8266 ssd1306 and dht22. My scope is to get value from request json weatherunderground and store in variables. I'm going crazy because i get the data from weather underground (class dict) but i don't know how to store value in variables:

Here is my code:
import urequests
r = urequests.get("http://api.wunderground.com/api/my.api/conditions/q/CA/San_Francisco.json").json() #getting data
print(type(r)) #give me class dict
print (r)
{'response': {'termsofService': 'http://www.wunderground.com/weather/api/d/terms.html', 'features': {'conditions': 1}, 'version': '0.1'}, 'current_observation': {'visibility_mi': '10.0', 'feelslike_string': '83 F (28 C)', 'precip_1hr_metric': ' 0', 'observation_epoch': '1505160367', 'dewpoint_c': 15, 'wind_degrees': 121, 'local_tz_offset': '-0700', 'dewpoint_f': 59, 'wind_string': 'From the ESE at 2.0 MPH Gusting to 6.0 MPH', 'wind_gust_mph': '6.0', 'forecast_url': 'http://www.wunderground.com/US/CA/San_Francisco.html', 'history_url': 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KCASANFR131', 'ob_url': 'http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.778488,-122.408005', 'observation_time': 'Last Updated on September 11, 1:06 PM PDT', 'pressure_trend': '0', 'wind_dir': 'ESE', 'wind_mph': 2.0, 'wind_gust_kph': '9.7', 'precip_today_string': '0.00 in (0 mm)', 'display_location': {'wmo': '99999', 'elevation': '60.0', 'city': 'San Francisco', 'longitude': '-122.41999817', 'magic': '1', 'country_iso3166': 'US', 'state': 'CA', 'zip': '94102', 'latitude': '37.77999878', 'country': 'US', 'state_name': 'California', 'full': 'San Francisco, CA'}, 'heat_index_c': 28, 'local_time_rfc822': 'Mon, 11 Sep 2017 13:06:32 -0700', 'image': {'title': 'Weather Underground', 'url': 'http://icons.wxug.com/graphics/wu2/logo_130x80.png', 'link': 'http://www.wunderground.com'}, 'icon_url': 'http://icons.wxug.com/i/c/k/partlycloudy.gif', 'estimated': {}, 'heat_index_f': 83, 'local_tz_short': 'PDT', 'heat_index_string': '83 F (28 C)', 'pressure_mb': '1012', 'visibility_km': '16.1', 'relative_humidity': '44%', 'precip_1hr_string': '0.00 in ( 0 mm)', 'windchill_f': 'NA', 'solarradiation': '972', 'wind_kph': 3.2, 'temperature_string': '83.0 F (28.3 C)', 'precip_today_metric': '0', 'windchill_c': 'NA', 'local_tz_long': 'America/Los_Angeles', 'station_id': 'KCASANFR131', 'icon': 'partlycloudy', 'observation_time_rfc822': 'Mon, 11 Sep 2017 13:06:07 -0700', 'nowcast': '', 'pressure_in': '29.88', 'UV': '4.7', 'dewpoint_string': '59 F (15 C)', 'local_epoch': '1505160392', 'observation_location': {'country': 'US', 'longitude': '-122.408005', 'state': 'California', 'city': 'SOMA, San Francisco', 'elevation': '23 ft', 'country_iso3166': 'US', 'full': 'SOMA, San Francisco, California', 'latitude': '37.778488'}, 'temp_f': 83.0, 'temp_c': 28.3, 'precip_today_in': '0.00', 'feelslike_c': '28', 'precip_1hr_in': '0.00', 'windchill_string': 'NA', 'feelslike_f': '83', 'weather': 'Partly Cloudy'}}

I don't know how getting determinate value to store in variables.

Thank you for all your support

B.R

Marcello

mwm
Posts: 22
Joined: Wed Aug 09, 2017 3:52 pm

Re: Json class dict store in variables

Postby mwm » Tue Sep 12, 2017 4:46 am

Your value is already stored in a variable - r. You did that the way you save values in Python, by writing 'r = ...', which evaluates the '...' and makes the variable r refer to that value (this is called binding, and it's not like assignment in C).

In particular, r is a dictionary, and the representation is a list of key: value pairs in curly braces. You pull individual values by appending a key in square brackets (i.e. - a subscript) to r, so r['response'] or r['current_observations'] are the two values in r, both of which are also dictionaries. So you subscript those as well, and something like r['current_observation'][heat_index_c'] is 28. There are other things you can do to dictionaries, look through the python documentation for a complete list. Or type dir(r) at the repl where r is defined to get a list.

If you want to make a variable refer to a value in a dictionary, you can just do another assignment to that value, using the dictionary name with the field name as a subscript.

If you want to automatically turn all the keys in a dictionary into variables with the same name that refer to the value, you should want something different. The problem is that anything is a valid as a key value, and you may not be able to use it as a variable name, so doing it is liable to generate a syntax error, and if you manage to make the assignment in spite of that a reference to the variable is a syntax error, and beyond that you can only refer to them if you know the field name anyway , so you might as well leave them in the dictionary. So there's no easy way to do this.

User avatar
pythoncoder
Posts: 1790
Joined: Fri Jul 18, 2014 8:01 am

Re: Json class dict store in variables

Postby pythoncoder » Tue Sep 12, 2017 6:43 am

You can access individual observations as follows:

Code: Select all

temperature = r['current_observation']['temp_c']
weather = r['current_observation']['weather']
wind_gust = r['current_observation']['wind_gust_kph']

And so on...
Peter Hinch

mahnonsaprei
Posts: 5
Joined: Sun Sep 03, 2017 1:43 pm

Re: Json class dict store in variables

Postby mahnonsaprei » Tue Sep 12, 2017 9:16 am

Thank you so much for all your replies.

Yes, i asked how to store a determinate value from this dict. Tonight i'll try and give you a feedback.

Thank you again.

;)

mahnonsaprei
Posts: 5
Joined: Sun Sep 03, 2017 1:43 pm

Re: Json class dict store in variables

Postby mahnonsaprei » Tue Sep 12, 2017 2:44 pm

Thank you for all your support. I try and it's worked!!!

mahnonsaprei
Posts: 5
Joined: Sun Sep 03, 2017 1:43 pm

Re: Json class dict store in variables

Postby mahnonsaprei » Wed Sep 13, 2017 10:16 am

Anothe question (if possible). I want to show cloud image on oled if are rainy days or sunny days ecc...
Can i get the image from json and convert in xbm format to show in SSD1306 ?

Sorry but i'm very noob in python.

Thank you so much for all your support.

Marcello

User avatar
pythoncoder
Posts: 1790
Joined: Fri Jul 18, 2014 8:01 am

Re: Json class dict store in variables

Postby pythoncoder » Wed Sep 13, 2017 4:46 pm

The official SSD1306 driver doesn't have a method for drawing graphics, so you'd need to subclass SSD1306_SPI or SSD1306_I2C as appropriate and write your own. Depending on your level of experience this task would range from "easy" to "quite difficult".
Peter Hinch

User avatar
deshipu
Posts: 1149
Joined: Thu May 28, 2015 5:54 pm

Re: Json class dict store in variables

Postby deshipu » Wed Sep 13, 2017 9:38 pm

pythoncoder wrote:The official SSD1306 driver doesn't have a method for drawing graphics, so you'd need to subclass SSD1306_SPI or SSD1306_I2C as appropriate and write your own. Depending on your level of experience this task would range from "easy" to "quite difficult".


That's not really true. The official SSD1306 driver uses the framebuf module, which supports drawing graphics. You just need to convert whatever graphics you are getting into a framebuf format. I believe someone already wrote a library for decoding PNG files, so this should be possible.

mahnonsaprei
Posts: 5
Joined: Sun Sep 03, 2017 1:43 pm

Re: Json class dict store in variables

Postby mahnonsaprei » Wed Sep 13, 2017 10:05 pm

Thank you for all of you support, i think that is too hard to deploy for my grade of knowledge.

Best regards

User avatar
pythoncoder
Posts: 1790
Joined: Fri Jul 18, 2014 8:01 am

Re: Json class dict store in variables

Postby pythoncoder » Thu Sep 14, 2017 5:56 am

deshipu wrote:...That's not really true. The official SSD1306 driver uses the framebuf module, which supports drawing graphics...

Good point. I should have thought of that :oops:

It does raise the question of whether the official driver should have one-line methods to expose the methods of the underlying framebuf (e.g. in this instance a blit() method). Such methods would make the capabilities of the driver more evident.
Peter Hinch


Return to “General Discussion and Questions”

Who is online

Users browsing this forum: No registered users and 1 guest