Insert Multiple JSON files to MongoDB using python

前端 未结 1 1200
滥情空心
滥情空心 2021-01-24 00:47

The JSON files are as follows a.json,b.json.....z.json (26 json files)

The json format of each of the file looks as:

{
    \"a cappella\": {
        \"w         


        
相关标签:
1条回答
  • 2021-01-24 01:40

    If you want to insert data from multiple .json files, do it in a loop:

    file_names = ['a.json', 'b.json', ...]
    
    for file_name in file_names:
        with open(file_name) as f:
            file_data = json.load(f)  # load data from JSON to dict
            for k, v in file_data.items():  # iterate over key-value pairs
                collection.insert_one(v)  # your collection object here
    
    0 讨论(0)
提交回复
热议问题