Load json

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
Mark.Calaway
Posts: 7
Joined: Sun Oct 29, 2017 4:22 pm

Load json

Post by Mark.Calaway » Sun Oct 29, 2017 4:29 pm

Hi guys,
I would like to load some parameters from a json file.
I wrote an example:

# mqtt.py :
import ujson
with open('document.json') as data_file:
data = ujson.loads(data_file)
yourWifiSSID = data["wifi"]["SSD"]
yourWifiPassword = data["wifi"]["password"]
print('SSD: {} & pass: {}',yourWifiSSID,yourWifiPassword)

And the json file:

# document.json :
{
"wifi": {
"SSD": "nome",
"password": "pass"
}
}

What i was wrong?

User avatar
benalb
Posts: 25
Joined: Fri May 19, 2017 1:23 pm

Re: Load json

Post by benalb » Mon Oct 30, 2017 11:05 am

Mark.Calaway wrote:
Sun Oct 29, 2017 4:29 pm
Hi guys,
I would like to load some parameters from a json file.
I wrote an example:

# mqtt.py :
import ujson
with open('document.json') as data_file:
data = ujson.loads(data_file)
(...)
What i was wrong?
The docs says: ujson.loads(str), Parse the JSON str and return an object. Raises ValueError if the string is not correctly formed.

so, you must pass the content of data_file as a string: data = ujson.loads(str(data_file.readlines()))

User avatar
Mark.Calaway
Posts: 7
Joined: Sun Oct 29, 2017 4:22 pm

Re: Load json

Post by Mark.Calaway » Mon Oct 30, 2017 4:21 pm

[quote="benalb"]
The docs says: ujson.loads(str), Parse the JSON str and return an object. Raises ValueError if the string is not correctly formed.

so, you must pass the content of data_file as a string: data = ujson.loads(str(data_file.readlines()))
[/quote]

Thx, i have another question: i need to close the file in the end?

with open('document.json') as data_file:
data = ujson.loads(str(data_file.readlines()))
data_file.closed
Last edited by Mark.Calaway on Mon Oct 30, 2017 8:27 pm, edited 2 times in total.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Load json

Post by SpotlightKid » Mon Oct 30, 2017 7:13 pm

This should be:

Code: Select all

with open(filename) as fp:
	data = ujson.loads(fp.read())
The readlines method of file objects returns the contents of the file as a list of lines (strings), which is not what we want here.

But contrary to the documentation, at least in the unix port of MicroPython, the ujson modul also supports the load function, which takes indeed a file object instead of the string, so that would be:

Code: Select all

with open(filename) as fp:
	data = ujson.load(fp)
Notice the missing trailing 's' in the function name.

User avatar
benalb
Posts: 25
Joined: Fri May 19, 2017 1:23 pm

Re: Load json

Post by benalb » Mon Oct 30, 2017 7:45 pm

SpotlightKid wrote:
Mon Oct 30, 2017 7:13 pm
This should be:
(... do not use readlines)

Code: Select all

with open(filename) as fp:
	data = ujson.load(fp)
I stand corrected. After test the code, I see that ujson.load is the correct way. :oops:

User avatar
Mark.Calaway
Posts: 7
Joined: Sun Oct 29, 2017 4:22 pm

Re: Load json

Post by Mark.Calaway » Mon Oct 30, 2017 8:07 pm

[quote=SpotlightKid post_id=22857 time=1509390816 user_id=552]
This should be:

[code]
with open(filename) as fp:
data = ujson.loads(fp.read())
[/code]

The [i]readlines[/i] method of file objects returns the contents of the file as a list of lines (strings), which is not what we want here.

But contrary to the documentation, at least in the unix port of MicroPython, the [i]ujson[/i] modul also supports the [i]load[/i] function, which takes indeed a file object instead of the string, so that would be:

[code]
with open(filename) as fp:
data = ujson.load(fp)
[/code]

Notice the missing trailing 's' in the function name.
[/quote]
Thx for your help.
They need to fix this mistake in the docs page.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Load json

Post by SpotlightKid » Mon Oct 30, 2017 9:01 pm

Thx, i have another question: i need to close the file in the end?
No, this is taken care of by the with statement, which makes fp a context manager, which closes the file at the end of the with-block.

Post Reply