Different approaches to build JSON in UiPath

I am trying to create a json body with this format:

{
“search”: [
{
“status”: “”
},
{
“order_task”: “”
},
{
“process_date”: “”,
“criteria”: “<”
},
{
“process_type”: “A”
}
],
“limit”: “1”
}

By making a list with objects with Append Item to list acitvity like this:

JObject.FromObject(
New With {
key .status = “”},
New With {
key .order_task = “”},
New With {
key .process_date = today,
key .criteria = “<”},
New With {
key .process_type = “A”})

And then creating the array like this:

JObject.FromObject(
New With {
key .search = JArray.FromObject(list),
key .limit = “1”}).ToString

When I create the list, I get the following error: BC30516: Overload resolution failed because no accessible ‘FromObject’ accepts this number of arguments.

How can I fix this?

Have you tried using the Dictionary for creating JSON?

  1. Initialize a dictionary with required type
    JsonDectionary = New Dictionary(Of String, String)

  2. Assign Key and value for the dictionary
    JsonDectionary("Status") = "Value"

  3. Convert dictionary to JSON using SerializeObject
    Newtonsoft.Json.JsonConvert.SerializeObject(JsonDectionary )

Creating a dictionary works for simple json body, this is the result I get with dictionary approach:

{
“status”: “value”,
“order_task”: " ",
“process_date”: “2024-03-18”,
“criteria”: “<”,
“process_type”: “A”,
“limit”: “1”
}

But I want the json object to be inside the “search” array" and “limit” object outside the array. How can I do this to get it on the format i want with dictionary then like this:

{
“search”: [
{
“status”: “”
},
{
“order_task”: “”
},
{
“process_date”: “”,
“criteria”: “<”
},
{
“process_type”: “A”
}
],
“limit”: “1”
}

Please refer the below workflow which would help you solve the problem in creating array of JSON objects.
Each dictionary(String, Object) will result in one JSON object. If you want to have array of JSON, then make a list of dictionaries and assign it to another dictionary.

DictionaryToJson.xaml (11.5 KB)

You can also use the below snippet to build your JSON using Invoke code


Try
	
	'Inner fields
	Dim statusJson As JObject = New JObject()
	statusJson.Add("status","")
	
	Dim order_task As JObject = New JObject()
	order_task.Add("order_task","")
	
	Dim process_date As JObject = New JObject()
	process_date.Add("process_date","")
	process_date.Add("criteria","<")
	
	Dim process_type As JObject = New JObject()
	process_type.Add("process_type","")
	
	'add fields to Json array
	Dim searchJsonArray As JArray = New JArray()

	searchJsonArray.Add(statusJson)
	searchJsonArray.Add(order_task)
	searchJsonArray.Add(process_date)
	searchJsonArray.Add(process_type)
	
	'building final json from the array and adding limit field
	Dim FinalJson As JObject = New JObject()
	FinalJson .Add("Search",searchJsonArray)
	FinalJson .Add("limit","1")
	
	Console.WriteLine(FinalJson.ToString)
	
Catch exc As Exception
	Console.WriteLine(exc.Message)
End Try

The first method creating a dictionary worked as I wanted. I am not so familiar with the invoke code acitvity. How would I be able to generate the json body to use in a http request activity from the code?

Please refer the below documentation for more details about Invoke Code actvity
Activities - Invoke Code (uipath.com)

You can create the JSON either using Dictionary or JObject, then convert it to string to use it in HTTP request.

Thank you for the help