0
filter = '[{"name":"MinPrice","value": "50"},{"name":"MaxPrice","value": "100"}]'

how can I convert this string to list? Not changing actual data.Data should not change

2 Answers2

0

First, please don't call it filter, as that shadows a very nice builtin identifier. I will rename as filt.

Use the json module:

>>> import json
>>> 
>>> filt = '[{"name":"MinPrice","value": "50"},{"name":"MaxPrice","value": "100"}]'
>>> lst = json.loads(filt)
>>> len(lst)
2
>>> lst[0]
{'name': 'MinPrice', 'value': '50'}
>>> 
>>> lst[1]
{'name': 'MaxPrice', 'value': '100'}
J_H
  • 9,521
  • 1
  • 16
  • 31
0

Try this:

>>> import ast
>>> f = '[{"name":"MinPrice","value": "50"},{"name":"MaxPrice","value": "100"}]'

>>> output = ast.literal_eval(f)
>>> type(output)
<class 'list'>
abhilb
  • 5,069
  • 2
  • 14
  • 24