Reading and writing JSON files

pandas can read and write data stored in the JavaScript Object Notation (JSON) format. This is one of my favorites due to its ability to be used across platforms and with many programming languages.

To demonstrate saving as JSON, we will save the Excel data we just read in to a JSON file and then take a look at the contents:

In [25]:
   # write the excel data to a JSON file
   df.head().to_json("data/stocks.json")
   !cat data/stocks.json # osx or Linux
   # !type data\stocks.json # windows

 {"Date":{"0":1405900800000,"1":1405641600000,"2":1405555200000,"3":1405468800000,"4":1405382400000}, "Open":{"0":83.46,"1":83.3,"2":84.35,"3":83.77,"4":84.3}, "High":{"0":83.53,"1":83.4,"2":84.63,"3":84.91,"4":84.38}, "Low":{"0":81.81,"1":82.52,"2":83.33,"3":83.66,"4":83.2}, ...

Get Learning pandas now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.