python - 如何使用 Python 将 MongoDB 的 bsondump 转换为 JSON?

标签 python json mongodb bson

所以我有大量来自 MongoDB 转储的 .bson。我正在使用 bsondump在命令行上,将输出作为标准输入传送到 python。这成功地从 BSON 转换为 'JSON' 但它实际上是一个字符串,而且看起来不是合法的 JSON。

例如,输入线路如下所示:

{ "_id" : ObjectId( "4d9b642b832a4c4fb2000000" ),
  "acted_at" : Date( 1302014955933 ),
  "created_at" : Date( 1302014955933 ),
  "updated_at" : Date( 1302014955933 ),
  "_platform_id" : 3,
  "guid" : 72106535190265857 }

我相信是Mongo Extended JSON .

当我读到这样一行时:

json_line = json.dumps(line)

我明白了:

"{ \"_id\" : ObjectId( \"4d9b642b832a4c4fb2000000\" ),
\"acted_at\" : Date( 1302014955933 ),
\"created_at\" : Date( 1302014955933 ),
\"updated_at\" : Date( 1302014955933 ),
\"_platform_id\" : 3,
\"guid\" : 72106535190265857 }\n"

仍然是 <type 'str'> .

我也试过

json_line = json.dumps(line, default=json_util.default)

(请参阅 pymongo json_util - 垃圾邮件检测阻止第三个链接) 这似乎与上面的转储相同。加载给出错误:

json_line = json.loads(line, object_hook=json_util.object_hook)
ValueError: No JSON object could be decoded

那么,如何将 TenGen JSON 字符串转换为可解析的 JSON? (最终目标是将制表符分隔的数据流式传输到另一个数据库)

最佳答案

您拥有的是 TenGen 模式下的 Mongo Extended JSON 转储(参见 here)。一些可能的方法:

  1. 如果可以再次转储,请通过 MongoDB REST API 使用严格输出模式。那应该给你真正的 JSON,而不是你现在拥有的。

  2. 使用 http://pypi.python.org/pypi/bson/ 中的 bson将您已经拥有的 BSON 读入 Python 数据结构,然后对这些数据结构进行您需要的任何处理(可能输出 JSON)。

  3. 使用 MongoDB Python 绑定(bind)连接到数据库以将数据导入 Python,然后执行您需要的任何处理。 (如果需要,您可以设置本地 MongoDB 实例并将转储文件导入其中。)

  4. 将 Mongo Extended JSON 从 TenGen 模式转换为 Strict 模式。您可以开发一个单独的过滤器来执行此操作(从 stdin 读取,将 TenGen 结构替换为 Strict 结构,并将结果输出到 stdout),或者您可以在处理输入时执行此操作。

这是一个使用 Python 和正则表达式的示例:

import json, re
from bson import json_util

with open("data.tengenjson", "rb") as f:
    # read the entire input; in a real application,
    # you would want to read a chunk at a time
    bsondata = f.read()

    # convert the TenGen JSON to Strict JSON
    # here, I just convert the ObjectId and Date structures,
    # but it's easy to extend to cover all structures listed at
    # http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON
    jsondata = re.sub(r'ObjectId\s*\(\s*\"(\S+)\"\s*\)',
                      r'{"$oid": "\1"}',
                      bsondata)
    jsondata = re.sub(r'Date\s*\(\s*(\S+)\s*\)',
                      r'{"$date": \1}',
                      jsondata)

    # now we can parse this as JSON, and use MongoDB's object_hook
    # function to get rich Python data structures inside a dictionary
    data = json.loads(jsondata, object_hook=json_util.object_hook)

    # just print the output for demonstration, along with the type
    print(data)
    print(type(data))

    # serialise to JSON and print
    print(json_util.dumps(data))

根据您的目标,其中之一应该是一个合理的起点。

关于python - 如何使用 Python 将 MongoDB 的 bsondump 转换为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11867538/

相关文章:

python - Mongoexport 导出无效的 json 文件

python - 附加到递归函数中的列表

python - Django - 调用函数不重定向

javascript - 如何将复杂的 JSON 值映射到简单对象数组

java - JSON 到 Java 对象

python - MemoryError 使用 json.dumps()

mongodb - 如何使用golang在mongodb中获取不同的值

python - 如何按类别从维基百科中抓取数据?

python - 如何扩展 Python 识别的时区列表?

arrays - 从子数组中找到文件?