How to use JSON with a Database

Some database management systems store data as JSON documents. Here's how you can add JSON to such a database.

There's been an insurgence of database management systems (DBMS) that use JSON to store data. While some of these are relational databases, most are from the NoSQL breed of DBMS. NoSQL databases typically use a different data model to the relational model that's been so popular for many years.

Database management systems that store data as JSON documents are often referred to as document store databases. They are also known as document-oriented database, aggregate database, or simply document store or document database.

How to use JSON with a Document Store Database

Each database management system has its own ways for inserting, updating, and retrieving data.

Document oriented databases store their data as documents. So this means, when you insert data into a document store, you're actually using the DBMS to create a JSON document.

The above statement will insert one document into the database. All the data provided is stored in one document.

Once the data is in the database, you can do things like:

Query it using MongoDB's find() method.

Update it using the update() or save() methods.

Delete it using any of the deleteOne(), deleteMany(), or remove() methods.

MongoDB also provides the mongoimport utility for importing JSON, CSV, or TSV files into a MongoDB database.

BSON

MongoDB actually stores the data in BSON format, which is an extension of JSON. BSON, which is short for Binary JSON, contains extensions that allow representation of data types that aren't supported by JSON. For example, BSON has a Date type.

BSON adds some extra information to documents, like length of strings and subobjects. This can speed up traversal.

Check out my MongoDB tutorial to see more about how JSON can work with document stores.

Relational Databases with JSON Support

Although many document oriented databases like MongoDB store data as JSON documents, not all do. Some use XML to store their data, but also have a certain level of support for JSON.