-2

For ex, I have this string:

"[{'id': 10402, 'name': 'Music'}, {'id': 18, 'name': 'Drama'}]"

I want to convert it into an actual list with dictionaries, how can I do it?

eaRobust
  • 87
  • 6
  • Have you tried [`json.loads`](https://docs.python.org/3/library/json.html#json.loads)? – 0x5453 Jun 02 '21 at 19:08
  • What have you tried? Stack Overflow won't write your code for you - see [ask]. – esqew Jun 02 '21 at 19:08
  • 1
    To maintain the quality of the content on Stack Overflow, we don't permit duplicate questions here. Please use the search function above (or your preferred search engine) to research your inquiry before posting here. This is a duplicate of [Convert JSON string to dict using Python](https://stackoverflow.com/questions/4528099/convert-json-string-to-dict-using-python) – esqew Jun 02 '21 at 19:09
  • Im sorry, JSON is not my goal format. I just want list of dictionaries – eaRobust Jun 02 '21 at 19:11
  • @eaRobust Can you elaborate on why the suggestions in the above comments don't meet your requirement of a "*list of dictionaries*"? I believe the linked duplicate addresses that directly. – esqew Jun 02 '21 at 19:13
  • 1
    @0x5453 That's not valid JSON; object keys must use double-quotes. – chepner Jun 02 '21 at 19:25

2 Answers2

0

What you (appear to) have is a Python list literal. You can use ast.literal_eval to parse it:

>>> from ast import literal_eval
>>> x = "[{'id': 10402, 'name': 'Music'}, {'id': 18, 'name': 'Drama'}]"
>>> literal_eval(x)
[{'id': 10402, 'name': 'Music'}, {'id': 18, 'name': 'Drama'}]

If the double quotes are actually part of your string, you can do the same, but the result will be a string literal that you would need to parse again:

>>> x = "\"[{'id': 10402, 'name': 'Music'}, {'id': 18, 'name': 'Drama'}]\""
>>> literal_eval(x)
"[{'id': 10402, 'name': 'Music'}, {'id': 18, 'name': 'Drama'}]"
>>> literal_eval(literal_eval(x))
[{'id': 10402, 'name': 'Music'}, {'id': 18, 'name': 'Drama'}]
chepner
  • 497,756
  • 71
  • 530
  • 681
0

You have a list of dictionaries encoded in a string with Python syntax:

string = "[{'id': 10402, 'name': 'Music'}, {'id': 18, 'name': 'Drama'}]"

To transform it into an actual list of dictionaries, the eval built-in function could be used:

eval(string)

That way, the string will be parsed, and a list containing the two dictionaries will be created.

But, please, note that the eval function is dangerous since any code can be evaluated. So it's safer to use the ast.literal_eval function for the simple task of creating that list of dictionaries:

import ast
ast.literal_eval(string)

The reason is that the string may only consist of simple literal structures (basically a very restricted subset of Python syntax).

Keep in mind that even ast.literal_eval is not completely safe, according to the corresponding documentation: It is possible to crash the Python interpreter with a sufficiently large/complex string due to stack depth limitations in Python’s AST compiler.

Marco Oliveira
  • 127
  • 1
  • 11