Key concepts for the Java API for JSON

Start of changeJSON, which is an abbreviation of JavaScript Object Notation, is a lightweight data-interchange format that is based on the object-literal notation of JavaScript. JSON is programming-language neutral but uses conventions from various languages. The Java™ API supports a JSON-oriented query language that is derived from the MongoDB query language. End of change

Tip: The Java API is no longer the recommended approach for working with JSON data in Db2 for z/OS®. For best results, use SQL and the built-in functions that are supplied with Db2 for z/OS instead. For more information, see Working with JSON documents by using SQL.

A JSON data store is a database that provides the capabilities to store, process, and manage data in JSON format. The JSON feature for Db2 enables a Db2 database to serve as a JSON data store. JSON documents in the data store are stored in a binary format, extended BSON.

JSON namespaces

Each JSON data store can support multiple JSON namespaces. JSON namespaces are conceptually similar to a MongoDB database and are represented as an SQL schema. By default, the namespaces are not case-sensitive.

In the command-line interface, the namespace is set with the use command and referenced with the command prefix db.

The following code sample represents a JSON namespace:
nosql>use media
Switched to schema MEDIA
nosql>db.getCollectionNames()
[movies, books, audio]

JSON collections

A JSON collection is a named grouping of JSON documents. Document structures in a collection might differ significantly. However, usually documents in a collection are of a similar nature to enable finding and grouping data.

Collections are represented in a Db2 table with a custom-defined or automatically generated unique identifier and a binary large object to hold the semi-structured document content. A collection can be created explicitly with the createCollection() command. A collection is implicitly created when an insert is attempted for a collection that did not previously exist.

The document identifiers that serve as primary key must be of the same data type for all documents in the collection.

JSON documents

JSON documents consist of fields, which are name-value pair objects. The fields can be in any order, and be nested or arranged in arrays.

There is no enforcement of document structures. Therefore, other documents in the same collection might have a subset of fields, extra fields, or different representations of the same field.

The keys, that is, the field names are always String data and must be unique. The values can be any of the supported JSON data types.
Table 1. JSON data types
Data type Example in JSON format
java.lang.String "string"
java.lang.Integer 3
java.lang.Long 4294967296
java.lang.Double 6.2
java.lang.Byte [] true / false
java.util.Date (millisecond precision, in UTC) { "$binary": "(base64-encoded value)", "$type": "0" }
java.util.regex.Pattern { "$date" : "1998-03-11T17:14:12.456Z" }
java.util.regex.Pattern { "$regex" : "ab*" , "$options" : "" }
java.util.UUID { "$uuid" : "fb46f9a6-13df-41a2-a4e3-c77e85e747dd" }
com.ibm.nosql.bson.types.ObjectId { "$oid" : "51d2f200eefac17ea91d6831" }
com.ibm.nosql.bson.types.Code { "$code" : "mycode" }
com.ibm.nosql.bson.types.CodeWScope { "$code" : "i=i+1", "$scope" : {} }
com.ibm.nosql.json.api.BasicDBObject { "a" : 1, "b": { "c" : 2 } }
com.ibm.nosql.json.api.BasicDBList [1 , 2, "3", "abc", 5]

For Date string values, the client converts the value to UTC to be stored in Db2 databases. It is also retrieved as a Date in UTC format.

The following example represents a sample JSON document:

{
	name:"Joe",
	age:25,
	phone:["555-666-7777", "444-789-1234"],
	homeAddress:
		{street:"Sycamore Avenue",
		city: “Gilroy”,
		zipcode:"95046"
		}
	businessAddress:
		{
		street:"Bailey Avenue",
		City: “San Jose”,
		zipcode:"95141"
		}
}

JSON nested objects

JSON objects can be nested inside other JSON objects. Each nested object must have a unique access path.

The same field name can occur in nested objects in the same document. However, uniqueness must still apply for the full access name.

To access nested fields, concatenate the field names that are separated by a . (dot). For example, use author.lastname to access the surname for the author in this document:

{"isbn": "123-456-222",  
 "author": 
    {
      "lastname": "Doe",
      "firstname": "Jane"
    },
"editor": 
    {
      "lastname": "Smith",
      "firstname": "Jane"
    },
  "title": "The Ultimate Database Study Guide",  
  "category": ["Non-Fiction", "Technology"]
 }

In the example, the field name lastname occurs in the author and the editor object. The prefixes in author.lastname and editor.lastname provide unique access.