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?
Load json
Re: Load json
The docs says: ujson.loads(str), Parse the JSON str and return an object. Raises ValueError if the string is not correctly formed.Mark.Calaway wrote: ↑Sun Oct 29, 2017 4:29 pmHi 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?
so, you must pass the content of data_file as a string: data = ujson.loads(str(data_file.readlines()))
- Mark.Calaway
- Posts: 7
- Joined: Sun Oct 29, 2017 4:22 pm
Re: Load json
[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
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.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Load json
This should be:
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:
Notice the missing trailing 's' in the function name.
Code: Select all
with open(filename) as fp:
data = ujson.loads(fp.read())
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)
Re: Load json
I stand corrected. After test the code, I see that ujson.load is the correct way.SpotlightKid wrote: ↑Mon Oct 30, 2017 7:13 pmThis should be:
(... do not use readlines)Code: Select all
with open(filename) as fp: data = ujson.load(fp)
- Mark.Calaway
- Posts: 7
- Joined: Sun Oct 29, 2017 4:22 pm
Re: Load json
[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.
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.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Load json
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.Thx, i have another question: i need to close the file in the end?