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

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots