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?

oren_isp
  • 729
  • 1
  • 7
  • 22

3 Answers3

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.

Graipher
  • 6,891
  • 27
  • 47
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']
}
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
  • Only use `eval` on text that you create yourself or confirm for yourself is safe, *never* from an unknown or untrusted source. – PaulMcG Dec 26 '18 at 15:43
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']}
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284