Sling Academy
Home/Python/MongoEngine: How to convert a document to JSON

MongoEngine: How to convert a document to JSON

Last updated: February 10, 2024

Introduction

MongoDB has become a popular choice for developers looking for a powerful, scalable, NoSQL database. MongoEngine, an Object Document Mapper (ODM) for Python, makes working with MongoDB documents in Pythonic way seamless. However, there are scenarios where converting these documents into JSON format is required, for example, to send data to a web client. This tutorial will guide you through various methods to convert MongoEngine documents to JSON, ranging from basic methods to more advanced uses.

Preparation

Prerequisite

Before diving into the conversion process, ensure that you have MongoDB installed and running on your local machine or server, and have MongoEngine installed in your Python environment. You can install MongoEngine using pip:

pip install mongoengine

Connecting to MongoDB

First, let’s connect our Python application to a MongoDB database using MongoEngine:

from mongoengine import connect

connect('mydatabase')

Defining a Document

To convert a document to JSON, we first need a document. Let’s define a simple user document:

from mongoengine import Document, StringField

class User(Document):
    name = StringField(required=True)
    email = StringField(required=True)

Creating and Saving a Document

Now, let’s create and save a user document:

new_user = User(name='John Doe', email='[email protected]')
new_user.save()

Basic Conversion to JSON

For a basic conversion of a single document to JSON, we can use the to_json() method provided by MongoEngine:

print(new_user.to_json())

This will output the document in JSON format:

{"_id": {"$oid": "unique_id"}, "name": "John Doe", "email": "[email protected]"}

Custom JSON Serialization

Sometimes, we need more control over the conversion process, especially when dealing with complex documents or when needing to exclude certain fields. Here’s how you can perform custom serialization:

from bson.json_util import dumps

json_data = dumps(new_user.to_mongo())
print(json_data)

This method allows you to use the flexibility of bson.json_util’s dumps() function for more complex needs.

Advanced: Converting Embedded Documents

Let’s assume our user has multiple addresses stored as embedded documents. Here’s how to manage this more complex scenario:

from mongoengine import EmbeddedDocument, EmbeddedDocumentField, ListField

class Address(EmbeddedDocument):
    street = StringField(required=True)
    city = StringField(required=True)

class User(Document):
    name = StringField(required=True)
    email = StringField(required=True)
    addresses = ListField(EmbeddedDocumentField(Address))

To convert a user document with embedded documents to JSON, use the following approach:

user = User(
    name='Jane Doe',
    email='[email protected]',
    addresses=[Address(street='123 Lorem Ipsum', city='Dolor Sit')]
)
user.save()

json_data = user.to_json()
print(json_data)

This includes the embedded documents in the JSON output, preserving the document structure.

Conclusion

Converting MongoEngine documents to JSON is straightforward thanks to the built-in methods and the flexibility offered by to_mongo() and dumps(). Whether dealing with simple or complex documents, these methods provide the tools needed to serialize MongoEngine documents into JSON, facilitating the creation of APIs and data interchange between systems. Embracing these techniques can significantly enhance the data handling capabilities of any MongoDB-backed Python application.

Next Article: Using Regular Expressions in MongoEngine

Previous Article: MongoEngine: How to update embedded documents

Series: Data Persistence in Python – Tutorials & Examples

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types