replace dictionary single quotes to double

Question:

dic = [{'one':1, 'two':'t', 'three': {'three.1': 3.1, 'three.2': '3.2' }}]

Desire Output,

[{"one":1, "two":"t", "three": {"three.1": 3.1, "three.2": "3.2" }}]

Tried Code:

ast.literal_eval(json.dumps(dic[0]))

here, Json converting the quotes but also changing the type to str. So I applied ast to change type. at end getting the same output with single quotes.

Output of Json.dumps: json.dumps(dic[0])

'{"one": 1, "two": "t", "three": {"three.1": 3.1, "three.2": "3.2"}}'

Above pre and post single quotes are the issue.

Asked By: user19903834

||

Answers:

Whats wrong with json.dumps(dic)?

Answered By: bitflip

If you want to print the dictionary to terminal with double quotes, you can

>>> print(json.dumps(dic))
[{"one": 1, "two": "t", "three": {"three.1": 3.1, "three.2": "3.2"}}]
Answered By: Tzane
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.