I am new to python. I have a string which is taken from my sqlite database. I want to convert the string to a list of list of dicts
. I tried ast
and json
library but it fails.
Here's the string:
a = '"[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]"'
Here the code i tried:
import ast
import json
a = '"[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]"'
a = a.replace("[[","[").replace("]]","]")
print(a)
# using json library- fails
# jdata = json.loads(a)
# for d in jdata:
# for key, value in d.iteritems():
# print (key, value)
#using ast library - it also fails
# res = [ast.literal_eval(x) for x in a]
I tried this link convert string to dict and convert str to list of list
But in my case, I have a list of list of dicts
which is in the form of string. How to make it possible.
I want the same as the output but it should be in list of list of dicts.
Required Output:
[[{"plugin_type":"input","plugin":"mysql","dbname":"smackcoders","user":"root","password":"root","tbname":"agg_csv","host":"localhost","id":"mysql1","limit_count":5},{"plugin_type":"filter","plugin":"metrics","input_from":"mysql1","id":"metrics","field_name":"count_result", "field":"state","value":"kerala","action":"count","send_data_immediately":True},{"plugin_type":"output","plugin":"elastic","id":"elastic_search","input_from":"metrics","ind":"neww10","doc_typ":"sm23"}]]