Convert PyMongo Cursor to JSON - GeeksforGeeks

26 May, 2020
Prerequisites: MongoDB Python Basics
This article is about converting the PyMongo Cursor to JSON. Functions like find() and find_one() returns the Cursor instance.
Let’s begin:
Importing Required Modules: Import the required module using the command:from pymongo import MongoClient
from bson.json_util import dumpsIf MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with PythonCreating a Connection: Now we had already imported the module, its time to establish a connection to the MongoDB server, presumably which is running on localhost (host name) at port 27017 (port number).client = MongoClient(‘localhost’, 27017)Accessing the Database: Since the connection to the MongoDB server is established. We can now create or use the existing database.mydatabase = client.name_of_the_databaseAccessing the Collection: We now select the collection from the database using the following syntax:collection_name = mydatabase.name_of_collectionGetting the documents: Getting all the documents from the collection using find() method. It returns the instance of the Cursor.cursor = collection_name.find()
Converting the Cursor to JSON: Converting the Cursor to the JSON.First, we will convert the Cursor to the list of dictionary.list_cur = list(cursor)Now, converting the list_cur to the JSON using the method dumps() from bson.json_utiljson_data = dumps(list_cur)
You can now save it to the file or can use it in the program using loads() function.
Importing Required Modules: Import the required module using the command:from pymongo import MongoClient
from bson.json_util import dumpsIf MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with Python
from pymongo import MongoClient
from bson.json_util import dumps
If MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with Python
Creating a Connection: Now we had already imported the module, its time to establish a connection to the MongoDB server, presumably which is running on localhost (host name) at port 27017 (port number).client = MongoClient(‘localhost’, 27017)
client = MongoClient(‘localhost’, 27017)
Accessing the Database: Since the connection to the MongoDB server is established. We can now create or use the existing database.mydatabase = client.name_of_the_database
mydatabase = client.name_of_the_database
Accessing the Collection: We now select the collection from the database using the following syntax:collection_name = mydatabase.name_of_collection
collection_name = mydatabase.name_of_collection
Getting the documents: Getting all the documents from the collection using find() method. It returns the instance of the Cursor.cursor = collection_name.find()

cursor = collection_name.find()

Converting the Cursor to JSON: Converting the Cursor to the JSON.First, we will convert the Cursor to the list of dictionary.list_cur = list(cursor)Now, converting the list_cur to the JSON using the method dumps() from bson.json_utiljson_data = dumps(list_cur)
You can now save it to the file or can use it in the program using loads() function.
list_cur = list(cursor)
Now, converting the list_cur to the JSON using the method dumps() from bson.json_util
json_data = dumps(list_cur)

You can now save it to the file or can use it in the program using loads() function.
Below is the implementation.
# Python Program for# demonstrating the # PyMongo Cursor to JSON     # Importing required modulesfrom pymongo import MongoClientfrom bson.json_util import dumps, loads    # Connecting to MongoDB server# client = MongoClient('host_name',# 'port_number')client = MongoClient('localhost', 27017)  # Connecting to the database named# GFGmydatabase = client.GFG   # Accessing the collection named# gfg_collectionmycollection = mydatabase.College  # Now creating a Cursor instance# using find() functioncursor = mycollection.find()  # Converting cursor to the list # of dictionarieslist_cur = list(cursor)  # Converting to the JSONjson_data = dumps(list_cur, indent = 2)    # Writing data to file data.jsonwith open('data.json', 'w') as file:    file.write(json_data)
Output:
Python-mongoDB
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
sum() function in Python