Home  >  Q&A  >  body text

通过Python将Json数据导入MongoDB

首先数据是以标准的json格式的文本。然后想要通过python脚本来导入Mongodb中。
json

{
    "service": "http", 
    "datetime": "2017-03-28 17:23:19", 
    "starttime": "1490692810", 
    "endtime": "1490692999", 
    "port": 80
}{  
    "service": "ewall", 
    "datetime": "2017-03-28 17:23:19",
    "starttime": "1490692810", 
    "endtime": "1490692999", 
    "port": 1328
}

python部分代码:

with open(filen, 'r') as f:
        while 1:
            try:
                jsonstr = f.readline().strip()
                # print jsonstr 可以输出整个json的内容
                if not jsonstr:
                    break
                try:
                    j = json.loads(jsonstr) #这里好像不处理的问题
                    
                except:
                    continue
                jsonlist.append(j)
            except:
                break

请问这个情况要怎么解决呢?谢谢

怪我咯怪我咯2741 days ago494

reply all(3)I'll reply

  • 阿神

    阿神2017-04-18 10:31:42

    Your problem is because yours is not in the standard json format. The standard format should be like this

    [{
        "service": "http", 
        "datetime": "2017-03-28 17:23:19", 
        "starttime": "1490692810", 
        "endtime": "1490692999", 
        "port": 80
    },
    {  
        "service": "ewall", 
        "datetime": "2017-03-28 17:23:19",
        "starttime": "1490692810", 
        "endtime": "1490692999", 
        "port": 1328
    }]

    Second, your data is read in rows. Please tell me what a row of your data looks like

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:31:42

    @sheep3’s answer is correct.

    If you put JSON directly into MongoDB, you can use mongoimport (https://docs.mongodb.com/manu...

    If you still want to process data, you can use code like this:

    import json
    filename = 'test.json'
    
    with open(filename, 'r') as f:
        content = json.load(f)

    If the content of the JSON file is larger than the memory, you should open the JSON file through streaming. You can use the ijson package (https://pypi.python.org/pypi/...). Usage is also relatively simple:

    import ijson
    
    with open('test.json') as fp:
        objects = ijson.items(fp, "item")
        for object in objects:
            print(object)

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:31:42

    @Christoph’s answer directly named a simpler and optimized solution, and I learned a trick

    reply
    0
  • Cancelreply