Using JSON in a UiPath automation

I’ve found myself using JSON more because JObject and the associated methods are actually quite simple, as is the JSON format, but also excellent for storing complex data in a structured string.

I built an automation that uses UiPath Forms, the Orchestrator API, and JSON to set the Queue Schema in Orchestrator and I will show how I handled the JSON.

For a little background the form looks like this:

And here is the resulting JSON:

Example JSON.txt (1.7 KB)

Now let’s look at how we load an existing Queue Schema as JSON:

First build a datatable:

image

Then read the text file:

image

Now we get into the JSON handling. First thing is to parse the JSON string into a JObject using: JObject.Parse(schemaJSONStr)


image

Then it’s a simple matter of referencing the JSON object’s properties to get the values. For example:

Assign schemaTitle = schemaJSONObj(“title”).ToString

I loop through the JSON object’s properties and add the necessary data for the form to a datatable.

Now let’s look at the opposite - taking the information in the form - some fields and a datatable - and turning it into JSON. I do this entirely in an Invoke Code because it’s much simpler than a series of Assign activities.

Here is the code to create the JSON object. First create jMain as JObject and then add the top level properties to it:

Dim jMain As JObject = New JObject

'add header section
jMain.Add("$schema","http://json-schema.org/draft-07/schema")
jMain.Add("$id","http://example.com/example.json")
jMain.Add("type","object")
jMain.Add("title",schemaTitle)
jMain.Add("description",schemaDescription)

'add default section
Dim jDefault As JObject = New JObject
jMain.Add("default",jDefault)

Then set up some additional JObjects and a JArray.

'build examples, required, properties
Dim jExample As JObject = New JObject
Dim jRequired As JArray = New JArray
Dim jProperties As JObject = New JObject

Then we loop through the datatable containing the fields and their settings. Because the “required” section is its own JObject, we start by adding the field’s name to the “required” section. For this we use the .Add method:

For Each CurrentRow As DataRow In DT_Properties.AsEnumerable
	'add to required section
	If CBool(CurrentRow("Required").ToString) Then
		jRequired.Add(CurrentRow("Field_Name").ToString)
	End If

Next step is to add the properties for the current field (remember we’re looping over them):

'build property
	Dim jProperty As JObject = New JObject
	Dim jPropertyExamples As JArray = New JArray
	jProperty.Add("$id","#/properties/" + CurrentRow("Field_Name").ToString)
	jProperty.Add("type",CurrentRow("Datatype").ToString)
	jProperty.Add("title","The " + CurrentRow("Field_Name").ToString + " schema")
	jProperty.Add("description",CurrentRow("Field_Name").ToString)
	Select Case CurrentRow("Datatype").ToString
	Case "string"
		JExample.Add(CurrentRow("Field_Name").ToString,CurrentRow("Example_Value").ToString)
		jPropertyExamples.Add(CurrentRow("Example_Value").ToString)
		If Not (CurrentRow("String_Format").ToString = "none" Or String.IsNullOrEmpty(CurrentRow("String_Format").ToString)) Then
			jProperty.Add("format",CurrentRow("String_Format").ToString)
		End If
	Case "integer"
		If Not String.IsNullOrEmpty(CurrentRow("Example_Value").ToString) Then
			JExample.Add(CurrentRow("Field_Name").ToString,CInt(CurrentRow("Example_Value").ToString))
			jPropertyExamples.Add(CInt(CurrentRow("Example_Value").ToString))
		End If
	End Select
	jProperties.Add(CurrentRow("Field_Name").ToString,jProperty)

Then we add the field’s examples and continue to the next field in the loop:

jProperty.Add("examples",jPropertyExamples)
Next

Next add the various objects we’ve built to the jMain object:

Dim jExamples As JArray = New JArray
jExamples.Add(jExample)
jMain.Add("examples",jExamples)
jMain.Add("required",jRequired)
jMain.Add("properties",jProperties)
jMain.Add("additionalProperties",additionalProperties)

And lastly convert the whole JObject to a string and assign it to the Invoke Code’s out argument:

schemaJSONStr = jMain.ToString

Depending on your needs you could also output the JObject itself to a JObject argument, but I didn’t need to here.

I hope this code example will help others with using JSON to store structured information. I now think of JSON like a super-dictionary - instead of just keys and values you have objects and their properties which can be other objects.

2 Likes