TAGS :Viewed: 4 - Published at: a few seconds ago

[ Have a number as identifier for a json ]

I have a (python) json that currently looks like this

{"templates":{"Main Screen":0,"dummy":1}}

what I want to have is

{"templates":{0:"Main Screen",1:"dummy"}}

but json would not decode it anymore

self.fileData=json.loads(self.VDfile.readlines()[0])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 15 (char 14)

any way around this?

thank you

Answer 1


What it seems like you really want is

{"templates":["Main Screen", "dummy"]}

In python, you can do like templates[0] or templates[1:], which is a better API than templates["0"] or templates.get("0").