3

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
4
  • 1
    @Graipher it is a string, see my edit Commented Dec 26, 2018 at 13:32
  • 3
    json.loads(string) ? Commented Dec 26, 2018 at 13:35
  • 2
    @Aprillion: It seems to be an invalid JSON string... Commented Dec 26, 2018 at 13:38
  • @Graipher simple replace the ' to " Commented Dec 26, 2018 at 14:01

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Only use eval on text that you create yourself or confirm for yourself is safe, never from an unknown or untrusted source.
0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.