Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python json dumps

Tags:

python

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 json.dumps(str_w_quotes) 

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]]}] 
like image 537
Delremm Avatar asked Mar 07 '13 13:03

Delremm


2 Answers

This works but doesn't seem too elegant

import json json.dumps(json.JSONDecoder().decode(str_w_quotes)) 
like image 120
slohr Avatar answered Sep 25 '22 03:09

slohr


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

import json json.dumps(json.load(str_w_quotes)) 

should give you:

 [{"id": 2, "name": "squats", "wrs": [["55", 9]]}] 
like image 29
Alex Spencer Avatar answered Sep 23 '22 03:09

Alex Spencer