-1

I have string representations of a dictionary like this:

s = "{'indexed': {'date-parts': [[2020, 9, 29]], 'date-time': '2020-09-29T19:03:40Z', 'timestamp': 1601406220500}}"

If I use json.loads(s) to convert to dict, I get an error as the value of date-parts is a list:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-183-a61a3a11c21f> in <module>
----> 1 json.loads(v)

/opt/anaconda3/envs/work_area/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

/opt/anaconda3/envs/work_area/lib/python3.7/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/opt/anaconda3/envs/work_area/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
    351         """
    352         try:
--> 353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
    355             raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

How do I solve this?

delvins78
  • 43
  • 7
  • 2
    Yeah, this is not JSON. If anything it's a Python literal, so could be parsed with `ast.literal_eval`. But… do you *expect* JSON? Then you should probably fix the code that produces this dict representation in the first place to actually produce JSON. – deceze Oct 26 '20 at 10:35
  • @deceze this is a excerpt from a long string. on review a bad example as `s.replace('"', "'") gives a dict. But doing this on the entire string produces an error as above. – delvins78 Oct 26 '20 at 11:22
  • @deceze `ast.literal_eval` works. – delvins78 Oct 26 '20 at 12:15

1 Answers1

0

It's because in your String you're using Apostrophe's instead of Quotation mark's

Small Suggestion google your errors. I typed

Expecting property name enclosed in double quotes Blockquote

Into Google and found this other question that answered your question Python/Json:Expecting property name enclosed in double quotes

Hladenz
  • 1
  • 2
  • @ Hladenz this is probably a bad example as replacing single quotes with double quotes gives a dict. I did Google, thanks. Thing is string is too long to post here, but error is as mentioned. – delvins78 Oct 26 '20 at 11:30