Previous Page Arrow Next Page Arrow

Formatting native objects in JSON

Representing a native data container in JSON is a straightforward task, guided by the following rules:

  • Scalar properties and their values are represented as JSON name-value pairs
  • Structural elements and sub-objects are represented as JSON objects
  • Collections are represented as JSON arrays

Expressing a simple data object in JSON

Let us consider a simplified SalesOrderLine object of the following structure and values:

  • productName -> “iPad”
  • productID -> 437
  • lineNumber -> 1
  • orderedQuantity -> 2
  • unitPrice -> 399.95

We note that the object contains scalar properties that translate easily into JSON name-value pairs. Thus, the JSON representation would be:

{ 
    "productName" : "iPad", 
    "productID" : "437",
    "lineNumber" : 1,
    "orderedQuantity" : 2, 
    "unitPrice" : 399.95
}

Expressing a data object with embedded structures in JSON

Let us alter the example above by grouping the product-related properties in an own structure:

  • product
    • name -> “iPad”
    • ID -> “437”
  • lineNumber -> 1
  • orderedQuantity -> 2
  • unitPrice -> 399.95

In this case, the product structure would be represented as a JSON object in its own right. Thus, the JSON representation is:

{
    "product" : { 
        "productName" : "iPad", 
        "productID"  : "437" 
    },
    "lineNumber" : 1,
    "orderedQuantity" : 2, 
    "unitPrice" : 399.95
}

Expressing an object with an embedded collection

A usual example of an object containing a collection of data elements is the SalesOrder. It contains a collection of line items for the individual positions in the order itself. A simplified SalesOrder structure is:

  • orderDate
  • shipDate
  • contact
    • firstName -> “John”
    • lastName -> “Doe”
    • email -> “john.doe@acme.com”
  • orderLines - containing
    • lineNumber -> 1
    • orderedQuantity -> 2
    • unitPrice -> 399.95
    • product
    • name -> “iPad”
    • ID -> 437
      and
    • lineNumber -> 2
    • orderedQuantity -> 1
    • unitPrice -> 323.00
    • product
    • name -> “Samsung Galaxy S2”
    • ID -> 932
    • subtotal -> 1021.95

In the translation we observe the following:

  • orderDate, shipDate and subtotal are scalar properties that should be represented as JSON name-value pairs
  • contact is a structural element, and therefore to be represented as a JSON object. The contact properties firstName, lastName and email are scalar properties expressed as name-value JSON pairs
  • orderLines is a collection, and should be represented as a JSON array. The array elements, the individual line items are structural elements translating into JSON objects.
  • product is a structural element, mapping to a JSON object
  • All remaining properties are scalar, hence representable as name-value JSON pairs.

The resulting JSON representation is:

{
    "orderDate" : "2001-07-01" , 
    "shippedDate" : null, 
    "contact" : {
        "firstName" : "John", 
        "lastName"  : "Doe", 
        "email"  : "john.doe@acme.com"
    }, // end contact
    "orderLines" : 
        [ {
            "lineNumber"  : 1,
            "orderedQuantity" : 2, 
            "unitPrice"  : 399.95,
            "product" : { 
                "name" : "iPad", 
                "ID"  : "437"
            } //end product
          }, {
            "lineNumber"  : 2,
            "orderedQuantity" : 1, 
            "unitPrice"  : 323.00,
            "product" : { 
                "name" : "Samsung Galaxy S2", 
                "ID"  : "932"
            } //end product
          } 
        ], // end orderLines
    "subtotal" : 1021.95
}

Previous Page Arrow Next Page Arrow