forked from k2zylstra/team-cyberhood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON_To_BSON.py
47 lines (31 loc) · 1.12 KB
/
JSON_To_BSON.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#JSON to BSON converter
import json
from bson import BSON
bad_json = '{"this is": "missing the closing bracket"'
try:
json.loads(bad_json)
except ValueError as error:
print ("json.loads() ValueError for BSON object:", error)
json_string = ""
with open("data.json TYPE:", 'r', encoding='utf-8') as json_data:
print ("data.json Type:", type(json_data))
for i, line in enumerate(json_data):
json_string += line
try:
json_docs = json.loads(json_string)
print ("json_docs TYPE:", type(json_docs))
print ("MongoDB collections:", list(json_docs.keys()))
except ValueError as error:
print ("json.loads() ValueError for BSON object:", error)
quit()
for key, val in json_docs.items():
for i, doc in enumerate(json_docs[key]):
try:
print ("\ndoc:", doc)
data = BSON.encode(doc)
print ("BSON encoded data:", type(data))
print ("data:", data)
decode_doc = BSON.decode(data)
print ("decode_doc:", type(decode_doc))
except Exception as error:
print ("enumerate() JSON documents ERROR:", error)