Writing JSON Documents

Let's discuss both the structure of JSON documents and the content of a collection of documents, say one held in a database.

There are three main areas to the consider: the basic document structure (fields and field types), the use of document types to track different document structures, and the structure and consistency of the documents that you store.

Document Structure

The main consideration is that because you can put all of the information about a single item into one document, you need a structure that is capable of defining and displaying that information. JSON is very flexible, but keep in mind that you also want to easily process the information.

Just like in a database that requires a schema, there are some conventions and field types that you should consider including in your documents.

Some good habits to get into:

Document Types*

If you are constrained to store all your documents in one database, you had better include a field called type or schema that identifies what the document contains. For example, in a blog enviroment you might store the blog posts and comments separately, so a blog post would like this:

{
  "title": "My Blog Post",
  "created_at": "2011-11-27T14:34",
  "content" : "My first blog post"
  "author" : "MC Brown",
  "schema" : "blogpost",
}

A comment should be identified (and formatted) differently:

{
  "schema": "comment",
  "blogdocid": "myblogpost",
  "from": "Joe Blog",
  "comment": "Good post!",
}

When writing views and searching and referring the content, the type or schema field can be used to identify the different document types and create different views and representations of the information accordingly.

Structure and Consistency

There are no constraints or schemas for a document database, but that doesn't mean that you can ignore aspects like the simplicity and consistency of the format. For example, there's no point in using a field for the title of a recipe if in one document the field name is title, recipetitle in another, and recipe_name in yet others.

It is a good idea to employ some basic consistency in your field names and contents to ensure you can cope with future changes and updates. I recomend using the same field names for all sorts of different data types across many different databases.

It is advisable to store and use a sample document that contains the basic document structure, which you can then use as a template or reference for your other documents.

ID's with _id

Both MongoDB and CouchDB add an ID field to the documents that they handle.