I have the following string:
"[['Categories', [['180972'], ['180800'], ['16228'], ['32733'], ['32789'], ['32833'], ['325137'], ['32839'], ['25329'], ['42605'], ['428240849'], ['5101'], ['568'], ['570716'], ['57116'], ['57080545404'], ['57083134076']]], ['Tags', ['Stock', 'Color', 'Fam', 'Dress','Maxi']], ['Type', ['Luxary']], ['Vendor', ['AAA']]]"
And I want to parse it as a dict/json. What is the best way to do so?
asked Dec 26, 2018 at 13:16
oren_isp
7792 gold badges8 silver badges23 bronze badges
3 Answers 3
You can use ast.literal_eval to evaluate a string and get back a Python object (if the syntax is correct). Using this is safer than using eval.
import ast
s = "[['Categories', [['180972'], ['180800'], ['16228'], ['32733'], ['32789'], ['32833'], ['325137'], ['32839'], ['25329'], ['42605'], ['428240849'], ['5101'], ['568'], ['570716'], ['57116'], ['57080545404'], ['57083134076']]], ['Tags', ['Stock', 'Color', 'Fam', 'Dress','Maxi']], ['Type', ['Luxary']], ['Vendor', ['AAA']]]"
l = ast.literal_eval(s)
d = dict(l)
{'Categories': [['180972'],
['180800'],
['16228'],
['32733'],
['32789'],
['32833'],
['325137'],
['32839'],
['25329'],
['42605'],
['428240849'],
['5101'],
['568'],
['570716'],
['57116'],
['57080545404'],
['57083134076']],
'Tags': ['Stock', 'Color', 'Fam', 'Dress', 'Maxi'],
'Type': ['Luxary'],
'Vendor': ['AAA']}
If you want to also get rid of the inner list, use the other answer, instead of just calling dict on the object.
answered Dec 26, 2018 at 13:35
Graipher
7,24630 silver badges49 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try this to convert it to dict:
data= "[['Categories', [['180972'], ['180800'], ['16228'], ['32733'], ['32789'], ['32833'], ['325137'], ['32839'], ['25329'], ['42605'], ['428240849'], ['5101'], ['568'], ['570716'], ['57116'], ['57080545404'], ['57083134076']]], ['Tags', ['Stock', 'Color', 'Fam', 'Dress','Maxi']], ['Type', ['Luxary']], ['Vendor', ['AAA']]]"
data = eval(data)
d={}
for i in data:
d[i[0]] = [x for x, in i[1]] if isinstance(i[1][0], list) else i[1]
The output will be:
{'Categories':
['180972',
'180800',
'16228',
'32733',
'32789',
'32833',
'325137',
'32839',
'25329',
'42605',
'428240849',
'5101',
'568',
'570716',
'57116',
'57080545404',
'57083134076'],
'Tags': ['Stock', 'Color', 'Fam', 'Dress', 'Maxi'],
'Type': ['Luxary'],
'Vendor': ['AAA']
}
answered Dec 26, 2018 at 13:34
Mehrdad Pedramfar
11.1k4 gold badges43 silver badges61 bronze badges
1 Comment
PaulMcG
Only use
eval on text that you create yourself or confirm for yourself is safe, never from an unknown or untrusted source.How about this
>>> import itertools
>>> import ast
>>> import pprint
>>> i = ast.literal_eval(s)
>>> d = {k[0]:list(itertools.chain(*k[1])) if isinstance(k[1][0], list) else list(k[1]) for k in i}
>>> pprint.pprint(d)
{'Categories': ['180972',
'180800',
'16228',
'32733',
'32789',
'32833',
'325137',
'32839',
'25329',
'42605',
'428240849',
'5101',
'568',
'570716',
'57116',
'57080545404',
'57083134076'],
'Tags': ['Stock', 'Color', 'Fam', 'Dress', 'Maxi'],
'Type': ['Luxary'],
'Vendor': ['AAA']}
answered Dec 26, 2018 at 13:47
Burhan Khalid
175k20 gold badges255 silver badges292 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-py
json.loads(string)?'to"