How to save nested dictionaries to a json file in python

python

#1

I´m trying to save a nested dictionaries structure to a json file and don´t know how to do it.

main(Dictionary) Key1: Dictionary1
key2: Dictionary2
… …

import json
with open(‘Main.json’, ‘w’) as fp:
json.dump(main, fp)

but it doesn´t work. Could you help me, please?


#2

please include the actual code you use, so we can pinpoint the problem


#3

The code is

import quandl
mydata = quandl.get("FSE/BAYN_X")
mydata2 = quandl.get("FSE/SIE_X")
temp={'Nombre':'BAYN','Datos':mydata}
main={'BAYN':temp}
temp={'Nombre':'SIE','Datos':mydata2}
main['SIEM']=temp

and then I try these different codes to save “main” to csv or json file


import csv
with open('Main.csv', 'wb') as csv_file:
    writer = csv.writer(csv_file)
    for key, value in main.items():
       writer.writerow([key, value])

or in json case


import json
with open('Main.json', 'w') as fp:
    json.dump(main, fp)

Thanks for your reply


#4

Not any change you can provide me with a small data set?

because with a normal dictionary:

import json
main = {"test":"test"}
with open('Main.json', 'w') as fp:
    json.dump(main, fp)
with open('Main.json', 'r') as fp:
  print fp.readline()

everything works fine.


#5

If I include a DataFrame inside the dictionary (mydata and mydata2) it gives me an error when I try to write it to a json file

TypeError: Object of type ‘DataFrame’ is not JSON serializable


#6

mydata and mydata2 DataFrames have the following structure:


             Open    High     Low   Close  Change  Traded Volume     Turnover  \
Date                                                                            
2017-07-27  110.5  110.65  107.20  108.45     NaN      4446867.0  482614685.0   
2017-07-28  107.2  107.70  106.45  107.70     NaN      2577718.0  276580879.0   
2017-07-31  107.8  108.30  106.90  107.15     NaN      1901128.0  204391824.0   
2017-08-01  107.6  107.80  106.35  107.50     NaN      1545384.0  165922983.0   
2017-08-02  107.2  107.55  105.90  106.20     NaN      1520276.0  161921852.0   

           Last Price of the Day Daily Traded Units  Daily Turnover  
Date                                                                 
2017-07-27                  None               None             NaN  
2017-07-28                  None               None             NaN  
2017-07-31                  None               None             NaN  
2017-08-01                  None               None             NaN  
2017-08-02                  None               None             NaN

#7

seems you need to convert dataframe to dictionary, but i have never done this.

use type() to see what data types you are dealing with

This is why including the essential details in the original topic is very important, if you would have, i wouldn’t have responded to it, given i have never done this, and let someone who does know handle it


#8

Thanks. This was my first post and didn´t know. I´ll do it for the next one.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.