Home > Mobile >  how to save several json locally?
how to save several json locally?

Time:03-17

I'm scraping a website trough it's api and I'm obtaining data in json format, given the quantity of data, i'd like to save it locally on my computer and elaborate it later. What is the best format to store several json? I've tried making a list of json in phyton and then converting it on csv with panda, but it seems to me that several keys have disappeared on the final file.

import pandas as pd

df = pd.DataFrame(my_list_of_json)
df.to_csv('Path/to/a/file/on/my/computer.csv', index=False) 

Is there a better way to do this? Maybe keeping a format closer to the original json? keeping in mind that I need to extract the data later?

CodePudding user response:

For analysis always keep the original data so store them as they are in .json.

You could of course simple create a folder and put all JSON files there but it would be better to use a Database e.g. one of the ones listed here.

CodePudding user response:

j = '''{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}'''
  
import json

#Load json string to json object
j_obj = json.loads(j)
#Write json to file
json.dump(j_obj, open("example.json", "w"))
#load json from file
j_obj_loaded = json.load(open("example.json", "r"))
  • Related