Post-relational data generation with DTM Data Generator. Part 2: JSON documents.

In the first article, we discussed the generation of simplest post-relational data - value arrays. JSON is a most popular data presentation format for web services and web back-end solutions. It allows users to define complex nested structures with minimum overhead. In the absence of internal validation methods, JSON is very flexible and easy to understand. All modern databases have JSON data type that helps users to store while JSON-document in the cell.

As you probably know, we offer DTM Data Generator for JSON but this tool is not suitable for traditional relational database population. It was designed for NoSQL databases like MongoDB or CouchDB. The tool offers no easy way to store results in PostgreSQL, MySQL or Oracle directly.

DTM Data Generator has three internal methods for JSON object or array generations. The first one is to use a pre-created set of files with a required structure. In other words, you can create a folder with JSON documents using DTM Data Generator for JSON and use it as a source. $BLOB function of internal data generation engine should be used for this case.

DTM Data Generator: $BLOB function for loading JSON files from folder with pre-defined data

The second way is suitable for documents with static structures only. The user can define the simple array or object as a pattern. In this case, the number of items is fixed. This way is not comfortable for complex structures but seems easy for documents with 3-5 elements.

DTM Data Generator: Direct template usage for JSON object creation.

Sample results:

DTM Data Generator: manually object generation results

The last way is most powerful but complex to learn. It is the built-in engine for JSON objects and arrays generation. The engine offers four functions:

  • MakeJSONArray creates JSON style array.
  • MakeJSONObject was designed for single object creation.
  • MakeJSONItem is the unified method for both objects and arrays.
  • MakeJSON is a glue for complex structures building.

Let's describe the mentioned functions in more details. The creation functions have ID as a first parameter. This ID can be used for reference. We'll provide you with samples below.

MakeJSONArray has 5 parameters. The first one is an ID. The second is the array name and it is optional. The engine will add "Name:" prefix before array if this parameter was specified:

The 3rd parameter defines how to use value: is it constant or pattern engine expression. The next one is value or reference. You should use "(expression)" for value or "[ID...]" for reference in this parameter. The last one is the repeater that defines a number or array items should be generated.

DTM Data Generator: JSON array creation with MakeJSONArray call.

Sample results:

DTM Data Generator: sample results of MakeJSONArray call

MakeJSONObject has the same parameters and one optional. It defines, should the engine use { and } for object encapsulation. MakeJSONItem is a wrapper for mentioned functions. It calls MakeJSONObject if the object name is defined as the second parameter or MakeJSONArray if not.

MakeJSON is a most complex function. It allows defining nested structures immediately. The function accepts a list of parameters. Each one defines an object or array in the same way as mentioned functions: MakeJSONObject and MakeJSONArray. The parameters for reference should be defined with '@' prefix.

Let's provide a few examples. As a sample we'll generate object and array:

$MakeJSON((1,Code,,($Rint(0,15)),,0))

This call generates a single-field object with filed name "Code" and a random number between 0 and 15 as a value.

$MakeJSON((1,Names,,[2],,0),@[2,,,($Lib(FirstNames)),5])

The call generates an object with filed name "Names" and an array of first names as a value:

DTM Data Generator: array produced by simple MakeJSON call

In the last example, we will generate an object with two items: names and countries. Each one will contain arrays of values:

$MakeJSON((1,Names,,[2],,0),@[2,,,($Lib(FirstNames)),5],(3,Countries,,[4],,0),@[4,,,($Lib(Countries,Code3)),=3])

The first parameter creates "Names" with reference to parameter 2, The third parameter creates object "Counties" with reference to parameter 4.

DTM Data Generator: two objects produced by MakeJSON call

In the next article of the series, we'll deal with XML data creation for database cells population purposes.