Python convert string representation of list to list (json.loads following json.dumps)

Question:

I have the following string that I need to sterilize with json.dumps, and then convert to list with json.loads.
The problem is that I cannot convert it to a list after json.dumps.
I even tried with ast.literal_eval().
The data type remains string:

test1 =  """[{"key":"value","found":"false","matches":"","correction":""}, 
                   {"keyb":"valueb","found":"false","matches":"","correction":""}]"""
a=json.dumps(test1)
b=json.loads(a)
print(type(b))  # str
Asked By: Rusty cole

||

Answers:

Try eval

test1 =  """[{"key":"value","found":"false","matches":"","correction":""}, 
           {"keyb":"valueb","found":"false","matches":"","correction":""}]"""
x = eval(test1)
print(x, type(x))

[{'key': 'value', 'found': 'false', 'matches': '', 'correction': ''}, {'keyb': 'valueb', 'found': 'false', 'matches': '', 'correction': ''}] <class 'list'>
Answered By: Abhi
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.