Q.on use of ujson

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
ebolisa
Posts: 55
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

Q.on use of ujson

Post by ebolisa » Mon Nov 29, 2021 10:53 pm

Hi,

The following code fails on an ESP because the data file contains a string. However, it runs on my win box.
Why is that?
TIA

Code: Select all

#alarm = [1, 2, 3, '4']
import ujson
data_file = 'data.json'
alarm = []
# with open(data_file, 'w') as f:
#     ujson.dump(alarm, f)
with open(data_file, 'r') as f:
    alarm = ujson.load(f)
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
ValueError: syntax error in JSON

User avatar
curt
Posts: 25
Joined: Thu Jul 29, 2021 3:52 am
Location: Big Lake, Alaska

Re: Q.on use of ujson

Post by curt » Mon Nov 29, 2021 11:29 pm

Are you sure the read is actually reading the entire file?

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

Re: Q.on use of ujson

Post by stijn » Tue Nov 30, 2021 7:13 am

Just a guess but have you tried with 'rb' instead of 'r'? Also: hard to tell without seeing the exact file contents.

User avatar
ebolisa
Posts: 55
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

Re: Q on use of ujson

Post by ebolisa » Tue Nov 30, 2021 7:31 am

Sorry for the late reply. It was bed time for me right after I posted the question.

The json syntax error drove me nuts till I found out it was the text datatype in the array the the culprit.

However, it's working this morning, so I can only guess the ESP needed to be cooled down :|

Post Reply