Data / Software Development / Sponsored / Contributed

Using Pydantic to Validate JSON Documents with Couchbase 

26 May 2022 6:14am, by

Couchbase Capella is a fully managed JSON document Database-as-a-Service (DBaaS) offering that eliminates database-management duties and reduces overall costs.

Rick Jacobs
Rick is technical marketing manager at Couchbase. He previously held roles at Cloudera; IC-CAP, LLC; Praescient Analytics; and IBM.

Capella offers robust performance, flexibility and scalability in a modern cloud native, distributed database that fuses the strengths of relational database features such as SQL and ACID transactions with JSON flexibility and scale that defines NoSQL.

Pydantic is a library that allows developers to define and validate custom JSON objects using Python type annotations. Pydantic accelerates coding time by enforcing type hints at runtime and raising user-friendly validation errors. The JSON documents generated using pydantic can then be used on the Capella platform without the concern for ill-formed documents that can occur when JSON documents are created via string standard concatenation.

To run the quickstart project, you need the following prerequisites:

  • Git repository URL: https://github.com/brickj/capella_pydantic
  • Python 3 installed
  • The pip package management tool
  • Installed Couchbase Capella SDK:
    • Full documentation is located here.
    • Example Python command: pip install couchbase
  • Installed pydantic Python library:
    • Full documentation is located here.
    • Example Python command: pip install pydantic
  • Couchbase Capella account:
  • Couchbase Capella cluster and bucket named “pydantic”

In this blog, I will quickly walk through an example of using pydantic to create valid JSON documents and storing them in Couchbase Capella.

The Couchbase Python SDK enables developers to perform CRUD operations on a specified bucket in a Couchbase cluster. For example, the following code snippet performs an upsert (insert if the document key is not in the bucket or update if the document already exists):

Python developers generally create JSON documents using strings or integers to populate a dictionary object that stores key-value pairs. That dictionary object is then sent to the specified bucket in the Capella database for insertion. The sample code to create and populate the dictionary object is similar to the snippet below:

With pydantic, developers have the flexibility to specify a schema for JSON objects that can be enforced as the documents are created. For example, the following steps through the creation of a JSON document for user posts. The document model looks like this:

Each UserPost object has the fields used in the post, but each post can have multiple reviews that need to be nested within the post. To enable this functionality, developers can create two separate classes for the UserPost and the Review. For example:

Once the objects are created, populating the documents is easy:

Now you can use the UserPost object as a dictionary and upsert it to Couchbase Capella using similar code to the upsert code shown earlier but using the object’s dict() method:

The full project illustrated in this blog includes the following:

  • Readme with instructions.
  • Code that creates the UserPost and Review objects.
  • Code that generates and prints a document to output.
  • Code that connects to Couchbase Capella using the Couchbase Python SDK.
  • Code that upserts the generated JSON document.

The public project can be cloned for Git at:https://github.com/brickj/capella_pydantic

Next Steps

To learn more about Couchbase Capella, our Database-as-a-Service offering:

At this point, you should be able to quickly use the pydantic library to create valid JSON documents to use with Couchbase Capella.

If you have any questions or comments, please connect with us on the Couchbase Forums!

Image by un-perfekt from Pixabay