My data looks like this:
Key1: [Key2:[{},{},...{}]]
The code something like this:
dict_={}
dict_['Key2']=all_items
dict_fin={}
dict_fin['Ke1']=[dict_]
df=pd.DataFrame(dict_fin)
df.to_json(filepath, orient="records",lines=True)
I would like to have saved json like this:
{ "Key1": [{"Key2" : [{},{},...,{}]}] }
Instead, I get it like this:
{ "Key1": {"Key2" : [{},{},...,{}]} }
2
Answers
I believe the errors is on the line
dict_fin['Ke1']=[dict_]
. You had assigneddict_['Key2']
instead:Use the
json
module from the Python standard library:Will give you:
'{"Key1": [{"inner_key": "inner_value"}, {"inner_key_2": "inner_value_2"}]}'