0

I'm having trouble converting the below JSON string to type dict. json.loads throws an error because of the two sets of squiggles: {}, {}

test = '{"124074": "0.0944", "124111": "0.0809", "124194": "0.0788"}, {"128213": "0.39", "129043": "0.458", "129054": "0.378"}'

Any thoughts on how I fix this? I need to be able to convert to dict so I can iterate through the keys. Should the JSON string be represented in a different way?

3
  • 1
    Just do json.loads("["+test+"]")? Commented Oct 31, 2021 at 4:25
  • 1
    @gre_gor agreed, or json.loads(f'[{test}]') should do it Commented Oct 31, 2021 at 4:56
  • Since that's not valid JSON, it raises the question... is there a bug in whatever generated it? Commented Oct 31, 2021 at 5:01

1 Answer 1

1

Based on the comment from @rv.kvetch, one can get the following result

>>> test = '{"124074": "0.0944", "124111": "0.0809", "124194": "0.0788"}, {"128213": "0.39", "129043": "0.458", "129054": "0.378"}'
>>> dicts = json.loads(f'[{test}]')
>>> dicts
[{'124074': '0.0944', '124111': '0.0809', '124194': '0.0788'}, {'128213': '0.39', '129043': '0.458', '129054': '0.378'}]
>>> dicts[0]
{'124074': '0.0944', '124111': '0.0809', '124194': '0.0788'}
>>> type(dicts[0])
<class 'dict'>

essentially a list of dicts is created instead of a single dict.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.