MongoDB ObjectId型別 序列化問題
阿新 • • 發佈:2020-08-21
MongoDB ObjectId型別 序列化問題
data = mongo的Bson資料
import json
from bson import json_util
data = json.loads(
json.dumps(
data,
sort_keys=False,
indent=4,
default=json_util.default)
)
JSON (JavaScript Object Notation)
http://json.org is a subset of
JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
interchange format.
Pretty printing::
>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
{
"4": 5,
"6": 7
}
json.dumps()
If ``indent`` is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. ``None`` is the most compact representation. ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. If *sort_keys* is true (default: ``False``), then the output of dictionaries will be sorted by key.