python json dumps

Each Answer to this Q is separated by one/two green lines.

i have the following string, need to turn it into a list without u”:

my_str = "[{u'name': u'squats', u'wrs': [[u'99', 8]], u'id': 2}]"

i can get rid of ” by using

import ast
str_w_quotes = ast.literal_eval(my_str)

then i do:

import json

and get

[{\"id\": 2, \"name\": \"squats\", \"wrs\": [[\"55\", 9]]}]

Is there a way to get rid of backslashes? the goal is:

[{"id": 2, "name": "squats", "wrs": [["55", 9]]}]

This works but doesn’t seem too elegant

import json

json.dumps thinks that the " is part of a the string, not part of the json formatting.

import json

should give you:

 [{"id": 2, "name": "squats", "wrs": [["55", 9]]}]

You don’t dump your string as JSON, rather you load your string as JSON.

import json

Your string is already in JSON format. You do not want to dump it as JSON again.

>>> "[{\"id\": 2, \"name\": \"squats\", \"wrs\": [[\"55\", 9]]}]".replace('\\"',"\"")
'[{"id": 2, "name": "squats", "wrs": [["55", 9]]}]'

note that you could just do this on the original string

>>> "[{u'name': u'squats', u'wrs': [[u'99', 8]], u'id': 2}]".replace("u\'","\'")
"[{'name': 'squats', 'wrs': [['99', 8]], 'id': 2}]"

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .