How to JSON

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Anonymmm
Posts: 9
Joined: Sat Jun 13, 2020 11:32 am

How to JSON

Post by Anonymmm » Sat Jun 13, 2020 11:44 am

Hi,
I am using a ESP32 dev kit board to run a simple wi-fi server using miropython. I would like to periodically store some values in the onboard flash memory. I have therefore created and added the file "config.json" to the ESP32, which contains the following:

Code: Select all

{
  "aa1":" ",
  "aa2":" ",
  "aa3":" ",
  "bb1":" ",
  "bb2":" ",
  "bb3":" ",
  "cc1":" ",
  "cc2":" ",
  "cc3":" ",
  "dd1":" ",
  "dd2":" ",
  "dd3":" ",
  "ee1":" ",
  "ee2":" ",
  "ee3":" "
  }
  
To get the values stored in the json file, I use this code:

Code: Select all

import json
with open('config.json') as f:
  config = json.load(f)

a1 = config['aa1']
a2 = config['aa2']
a3 = config['aa3']
b1 = config['bb1']
b2 = config['bb2']
b3 = config['bb3']
c1 = config['cc1']
c2 = config['cc2']
c3 = config['cc3']
d1 = config['dd1']
d2 = config['dd2']
d3 = config['dd3']
e1 = config['ee1']
e2 = config['ee2']
e3 = config['ee3']
So far so good. My program than changes the values of a1,a2,a3,b1 and so on. Next, I put these values back into config, as so:

Code: Select all

  config['aa1'] = a1
  config['aa2'] = a2
  config['aa3'] = a3
  config['bb1'] = b1
  config['bb2'] = b2
  config['bb3'] = b3
  config['cc1'] = c1
  config['cc2'] = c2
  config['cc3'] = c3
  config['dd1'] = d1
  config['dd2'] = d2
  config['dd3'] = d3
  config['ee1'] = e1
  config['ee2'] = e2
  config['ee3'] = e3
Now, I would like to upload config to the config.json file, so the new values are there. How do I do this? I feel like I have now read and tried everything on json that is availiable online, yet nothing has worked. Any help is really appreciated!

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: How to JSON

Post by stijn » Sat Jun 13, 2020 12:45 pm

I think you're looking for json.dump to write values to a file: https://docs.python.org/3/library/json.html
Also if you have a series of values it might be more interesting to use a dict/list instead of all individual values, like

Code: Select all

values = json.loads('{"aa": ["someValue", " ", " "], "bb": [" ", " ", " "]}')
firstValueFromAA = values['aa'][0]  #firstValueFromAA is now 'someValue'
#Change second value in aa list
values['aa'][1] = 'abc'
# Write back to file
with open('/path/tomyfile', 'wb') as f:
  json.dump(values, f)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to JSON

Post by pythoncoder » Sat Jun 13, 2020 12:48 pm

Use json.dump:

Code: Select all

>>> import json
>>> d = {'aa1': '', 'aa2': ''}
>>> d['aa1'] = 22
>>> d['aa2'] = 33
>>> print(d)
{'aa1': 22, 'aa2': 33}
>>> with open('foo.dat', 'w') as f:
...     json.dump(d, f)
... 
>>> 
[adminpete@capybara]: ~
$ cat foo.dat
{"aa1": 22, "aa2": 33}[adminpete@capybara]: ~
$ 
Getting it back is as you've found

Code: Select all

>>> import json
>>> with open('foo.dat', 'r') as f:
...     d = json.load(f)
... 
>>> print(d)
{'aa1': 22, 'aa2': 33}
>>> 
Peter Hinch
Index to my micropython libraries.

Anonymmm
Posts: 9
Joined: Sat Jun 13, 2020 11:32 am

Re: How to JSON

Post by Anonymmm » Mon Jun 15, 2020 8:24 am

Damn, I spend a day on this, and you guys make it work in literally seconds. Thank you so much! Out of curiosity, may I ask what the 'w' is for in:

Code: Select all

with open('config.json', 'w') as f:
    json.dump(config, f)    
I think that´s what I was missing... anyway, thanks again!

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: How to JSON

Post by stijn » Mon Jun 15, 2020 10:05 am


Anonymmm
Posts: 9
Joined: Sat Jun 13, 2020 11:32 am

Re: How to JSON

Post by Anonymmm » Mon Jun 15, 2020 1:54 pm

Thanks!

Post Reply