0

I have a function to_json in a class that will return a string of JSON like this:

{'clusters':[{ 'host':'hostj', 'name':'s3', 'port':'poorke', 'profile':'profil', 'region':'regieo', 'user':'userk' }]}

Now I want to create a dict from it. When I use folowwing code :

new_cluser_dict = json.loads(clusterx.to_JSON())

I get following error :

Traceback (most recent call last):
  File "/Users/stevengerrits/anaconda/envs/myenv/lib/python3.4/site-packages/IPython/core/interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-13-0887c8f07b97>", line 1, in <module>
    newdict = json.loads(clusterx.to_JSON())
  File "/Users/stevengerrits/anaconda/envs/myenv/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/Users/stevengerrits/anaconda/envs/myenv/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/stevengerrits/anaconda/envs/myenv/lib/python3.4/json/decoder.py", line 359, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
EdChum
  • 376,765
  • 198
  • 813
  • 562
user5488652
  • 421
  • 3
  • 8
  • 15
  • Don't generate json by hand, use the `json` module from the stdlib instead. And FWIW, you just **don't** need json at all in your above example - what you need is a `clusterx.to_dict()` method that returns a plain python `dict`. – bruno desthuilliers Jan 18 '16 at 12:38

1 Answers1

0

You need to replace single quotes to double quotes:

clusterx.to_JSON().replace("'", '"')

Similar question: python: single vs double quotes in JSON

AndreyT
  • 1,449
  • 1
  • 15
  • 28