Print ( "BSON encoded data:", type (data ) ) # encode the document using the BSON library The following script will execute a try-catch error indentation block to verify the JSON string is valid before attempting to encode it as a BSON object: If the JSON string is not valid, then the JSON library will throw a ValueError exception. The json.loads() method requires passing a valid JSON string to its method call. Understanding a ValueError exception example raised by json.loads() How to Validate the JSON String and Create a Python Dictionary from the MongoDB DocumentsĪfter the string is loaded into the Python script it must be converted into a valid JSON Python dict before the string can be encoded into a BSON object. NOTE: Make sure to pass the complete file path in the file name string if the JSON file is not in the same directory path as the Python script. # append the parsed IO string to the JSON string # iterate over the _io.TextIOWrapper returned by open() using enumerate() Print ( "data.json TYPE:", type (json_data ) ) With open ( "data.json", 'r', encoding = 'utf-8' ) as json_data: ![]() ![]() # use Python's open() function to load a JSON file
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |