← index #18198Issue #17141
Off-topic · high · value 0.472
QUERY · ISSUE

JSON not converting namedtuple to list

openby billbreitopened 2025-10-03updated 2025-10-03
bug

Port, board and/or hardware

Pico 2

MicroPython version

JSON correctly converts namedtuples to lists when they appear in list form, but not when used as value in dictionary. Seems to work the same on mpy verion 1.20 - 1.26, running on Pico, Pico2 , Arduino ESP32.

Work around by manually converting.

Reproduction

MicroPython 1.20 - 1.26

dict[str,list[tuple] {'a': [(1, 2, 3)], 'b': [(4, 5, 6)]}
dict[str,list[namedtuple] {'a': [TT(x=1, y=2, z=3)], 'b': [TT(x=4, y=5, z=6)]}

json.dumps tuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}
json.dumps namedtuples {"a": [TT(x=1, y=2, z=3)], "b": [TT(x=4, y=5, z=6)]}

Python 3.9

dict[str,list[tuple] {'a': [(1, 2, 3)], 'b': [(4, 5, 6)]}
dict[str,list[namedtuple] {'a': [TT(x=1, y=2, z=3)], 'b': [TT(x=4, y=5, z=6)]}

json.dumps tuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}
json.dumps namedtuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}

Expected behaviour

json.dumps namedtuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}

Observed behaviour

json.dumps namedtuples {"a": [TT(x=1, y=2, z=3)], "b": [TT(x=4, y=5, z=6)]}

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

Json parser accepts uncomplete json objects

closedby aylen384opened 2025-04-16updated 2025-05-20
bug

Port, board and/or hardware

rp2, unix

MicroPython version

1.24.1

Reproduction

Micropython

>>> buffer = b'{"getEnergy_0": 977.222, "getVelocity_1": 0.0, "getHeading_0": 1'
>>> import json
>>> json.loads(buffer)
{'getHeading_0': 1, 'getEnergy_0': 977.222, 'getVelocity_1': 0.0}

Expected behaviour

cPython:

>>> buffer = b'{"getEnergy_0": 977.222, "getVelocity_1": 0.0, "getHeading_0": 1'
>>> import json
>>> json.loads(buffer)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    json.loads(buffer)
    ~~~~~~~~~~^^^^^^^^
  File "/usr/lib64/python3.13/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "/usr/lib64/python3.13/json/decoder.py", line 345, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.13/json/decoder.py", line 361, in raw_decode
    obj, end = self.scan_once(s, idx)
               ~~~~~~~~~~~~~~^^^^^^^^
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 65 (char 64)

Observed behaviour

See above

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied