Create Types From JSON Schema

Last Updated: 07/15/2016 Introduced in Verision: 2.0

JSON Schema defines the media type “application/schema+json”, a JSON based format for defining the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. JSON Schema is intended to define validation, documentation, hyperlink navigation, and interaction control of JSON data. For more information please visit http://json-schema.org

The key difference between JSON Schema and JSON type is that JSON Schema defines data type for properties. For instance, if we receive data from JSON object that looks like “2”, it is hard to tell what type of data it is (char, int, string). On the other hand, JSON Schema will explicitly define type for data that we should receive from JSON object. Creating Types From JSON Schema feature was implemented in Decisions 3.2.0 version. This tutorial shows how to create and use types from JSON Schema.

 

Example:

In this example we will create Type From JSON Schema and use this Schema to deserialize an array of JSON objects, display them on the Form, add new objects to the array, serialize, and return a String that represents an updated array of JSON objects.

We begin in the Designer Folder with selecting Datatypes/Database > XML/JSON > Create Types from Json Schema from Folder Actions Panel.

 addJsonSchemaType

In the resulting pop-up window we provide our Schema with a Name and either use Json Schema File or Paste Schema Code. In this example we paste Json Schema. When finished, click Ok.

 defineSchema

Mention, system creates Json Schema Data Structure along with Simple Flow Structure based on our Schema.

 flowDataStructCreated

Next, we can start creating our Flow where we will use Json Schema Type. From Folder Actions Panel click Create Flow button.

 createFlow

Then, we Name our Flow and click Create to proceed to the Flow Designer.

 nameFlow

In the Flow Designer we add Deserialize PersonJsonSchema step from Integrations > All Integrations > Json Schema Structures > PersonJsonSchema category.

 addDeserializeStep

Next, we select Deserialize PersonJsonSchema step on the workspace and Step Information Layer pops up. We can configure this step within this Layer. First, we make sure that IsOutputArray checkbox is checked because we are going to work with an array of JSON objects in this example. Then, we define Constant Mapping Type for String To Deserialize Input and paste the String that represents an array of JSON objects into Value text box. These JSON objects fit in description that we defined in our JSON Schema Type (the input for this step may be a result of Webservice call).

 addJsonStringToDeserialize

Then, we connect Error outcome from Deserialize PersonJsonSchema step to the End step in our Flow and add Show Form step from the Favorite Steps category on the Done outcome.

 addForm

In the resulting window we Name our Form and click Create to proceed to the Form Designer.

 createForm

In the Form Designer we use Data Grid component from the List category in the ToolBox to display deserialized results from the array of JSON objects. For this component we pick PersonJsonSchema type that we defined at the beginning of this tutorial.

 pickTypeForDataGrid

At the end our Form Design looks as following… We added Grid Action Buttons from the List category in the ToolBox to Add and Remove objects. Also, we provided our Grid component with a Data Name for inputs, configured Grid Mode as Output All, and defined a Name for Output Data. When finished, we can save the Form and close Form Designer.

 formeDesignCompleted

Back in the Flow Designer we add Create Data step from Favorite Steps category next to our Form step.

 createDataFirst

In the resulting pop-up window we click Add New link to add Data Definition to this step.

 sddNewFirstCreateData

In the Add Data Definition window we Name our data, define its type as String and check Is List and Can Be Null checkboxes. Save and close this Data Definition window.

 dataDefinitionAddedCreateDataFirst

Next, we are going to iterate through the updated collection of deserialized JSON objects, serialize them, and add to the List that we created in the previous step of this tutorial. We add For Each Element step from Favorite Steps category next to Create Data step in our Flow.

 addForEach

Then, we pick a collection for our For Each Element step. In this case we pick an updated collection of deserialized JSON objects which outcomes from our Form. Click Add to continue.

 pickCollectionForEach

In this example we changed the Outcome Data Name in the For Each Element step from Item to DeserializedPerson. Click Add Step to continue building the Flow.

 renameForEachOutcome

On the Next outcome from the For Each Element step we add Serialize PersonJsonSchema step from the Integrations > All Integrations > Json Schema Structures > PersonJsonSchema category.

 addSerializeStep

Then, we connect Error outcome from Serialize PersonJsonSchema step to the End step in our Flow. For the Object to Serialize input in this step we configure Select Value Mapping Type and using Path picker we select DeserializedPerson outcome from the For Each Element step.

 pickObjToSerialize

Next, on the Done outcome from the Serialize PersonJsonSchema step we add Add Item to List step from the All Steps [Catalog] > Data > List category.

 addItemToList

Then, we connect Done outcome from the Add Item to List step back to the For Each Element step in our Flow. Select Show Mapping Editor option for Add Item to List step.

 showMappingAddItemToList

In the Mapping Editor we connect the Output from Serialize PersonJsonSchema step to the New Item field and ListOfSerializedPersons that we defined with Create Data step to the Original List field. In the Outputs section in the Mapping Editor we use Change Value option for the Output and connect it back to the ListOfSerializedPersons. In this way our List will be updated after each iterations. When finished, we can click Ok to save and close Mapping Editor.

 mappingForAddItemToList

To define input data in our Form we select Show Form step on the workspace and Step Information Layer pops up. We configure Select Value Mapping Type for Persons Data Grid component and using Path picker we select Output Data from Deserialize PersonJsonSchema step.

 inputMappingForForm

Next, on the Done outcome from the For Each Element in our Flow we add Join Strings step from All Steps [Catalog] > Data > Text category.

 addJoinStrings

This step configured as following… We use Constant comma as a separator and our ListOfSerializedPersons as a source.

 mappingForJoinStrings

On the Done outcome from the Join Strings step we add another Create Data step from Favorite Steps category.

 createDataSecond

In this step we Define a String Data. This String will represent the updated array of JSON objects.

 createDataSecondDefinition

When we finished defining Data for this Create Data step, we connect the outcome from this step to the End step in our Flow. For the String that we defined in this step we use Text Merge.Plain Mapping Type and click Show Editor.

 textMergePlain

In the Merge Text Editor  we simply surround the output from Join Strings step with square brackets. Click Ok to save and close Merge Text Editor.

 textMergeEditor

We connect steps in our Flow in the following manner and click Debug Flow link to test our Flow.

Note that in Decisions version 3.5 and above, you’ll need to click Test Flow to access the Debugger.

3.5 Test Flow Shot

 debugFlow

In the Debugger our Form opens and Data Grid component displays deserialized JSON objects. We click Add Person to add object to the Data Grid.

 addPersonInDebugger

Next, we fill in all the fields for the new row and click Ok.

 personAdded

New row is added to the Data Grid and we click Done to continue Flow execution.

 clickDone

Debugger Diagram indicates that our Flow ran to the End step with no issues. If we look up the Output Data from the last step in our Flow, we can confirm that the output is an updated array of JSON objects. We can use this String to create a new JSON file but this is out of this tutorial’s scope.

outputDataResult

Additional Resources