Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import re
- import json
- import tempfile
- from bson import json_util
- from pprint import pprint
- example = \
- """{
- "_id" : ObjectId("5baca841d25ce14b7d3d017c"),
- "country" : "in",
- "state" : "",
- "date" : ISODate("1902-01-31T00:00:00.000Z")
- }"""
- def read_mongoextjson_file(filename):
- with open(filename, "r") 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'ISODate\s*\(\s*(\S+)\s*\)',
- r'{"$date": \1}',
- jsondata)
- jsondata = re.sub(r'NumberInt\s*\(\s*(\S+)\s*\)',
- r'{"$numberInt": "\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)
- return data
- if __name__ == '__main__':
- with tempfile.NamedTemporaryFile(mode='w+') as temp:
- temp.write(example)
- temp.flush()
- data = read_mongoextjson_file(temp.name)
- print('data is of type: {}'.format(type(data)))
- pprint(data)
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement