How do I create a JSON string from an Array?

Hello, So I see plenty of examples on how to take an JSON string the bot got from another service and deserialize it into an array for it to process.

But I’m trying to go in the other direction. My bot has gathered various bits of information into an array and I want to encode that into a single JSON string that it can then pass out to the next service to handle.

Hi @Kit_Ramos

You could try to loop through your array and save the data following json syntax. It shouldn’t be that complicated.

Hi, @loginerror
Do you have any example of what you wrote?

Unfortunately, I do not have it on hand.

I did find a feature request with a suggestion on how to proceed:

You could use the JSONConvert.SerializeObject() from Newtonsoft.Json together with the Invoke Code activity.

I hope it sets you on the right track.

This can be done with an Assign activity, but first you need to import Newtonsoft.Json into your project:

  1. Click the “Imports” tab at the bottom of the screen (it’s down with the Variables and Arguments tabs).
  2. Type Newtonsoft.Json and press enter.

Now a single Assign will do the trick:

      stringVar = JsonConvert.SerializeObject(arrayVar)


Here’s the sequence I used to test; my project imports Newtonsoft.Json as shown above:

Sequence variables

  • myArray: array of String
  • myArrayAsJson: String

Sequence activities

  1. Assign: myArray = {"abc", "def", "ghi"}
  2. Assign: myArrayAsJson = JsonConvert.SerializeObject(myArray)
  3. Write Line: "JSON = " + myArrayAsJson

Run and check the Output panel, and you’ll see this message:

      JSON = ["abc","def","ghi"]

This works for Dictionaries and Lists too.

5 Likes

Hello, i want to iterate trough this json list :
{
“url”: [“url1”, “url2”, “url3”]
}
by using deserialize json , can u help ?

Use the Deserialize JSON activity for this. If you can’t find the activity it means you have to install the UiPath.Web.Activities package using Manage Packages.

The result is a Newtonsoft.Json.Linq.JObject object. There’s a good general example of querying a complicated JObject instance here, but for your specific question you just want to navigate to the url property (which is an array) and then iterate through the array. This sequence will print the array values to the output window:

Sequence variables

  • jsonString: type = String
  • urlObject: type = JObject (Newtonsoft.Json.Linq.JObject)

Sequence activities

  • Assign jsonString = "{""url"":[""url1"",""url2"",""url3""]}"

  • Deserialize JSON

    • Set Input / JsonString = jsonString

    • Set Output / JsonObject = urlObject

      image

  • For Each

    • For each item in urlObject("url") (this points at the “url”:[…] array, which is a collection of JValue objects)

      image

    • Set Misc / TypeArgument to Newtonsoft.Json.Linq.JValue

      image

  • Inside the For Each Activity: Write Line

    • item.ToString()

      image

And here’s the output:

image

2 Likes

thank you so much @EdGibbs it works very well

1 Like

what if i have data into excel so how can i do it ?
@EdGibbs

Hi @EdGibbs,

I have a scenario where my Json String format is not fixed. Dor a particular key I have an array of key-value pairs also their count is not fixed. For each transaction it is going to be different . All would have different values as well.
Below is the example of Json String:
{
“key1” : “value1”,
“key2” : “value2”,
“key3” : “value3”,
“key4”:[
{
“key4.1” :“value5”,
“key4.2” :“value6”
}

{
  "key4.1" :"value1",
  "key4.2" : "value2"
}

],
key5” :[
{
“key’5.1” : “value9”,
“key5.2” : “value 10”
}

{
“key5.1” : “value9”,
“key5.2” : “value 10”

}

Here count of array items for key4 and key5 are dynamic along with their content.

Any help is appreciated.
Thanks in advance!

Hi @rinki

You might want to look into ways of creating a Json object from scratch then, see here:
https://www.newtonsoft.com/json/help/html/CreatingLINQtoJSON.htm

Simple example. This code:

New JObject(
        New JProperty("key","value")
		).ToString

Will give you an actual JSON object you want, like so (here I just output it to console by using .ToString):

Additionally, if you want dynamic arrays, you can first create an array and then add an element to it by using Invoke Method activity like so:

(1) Create source Jarray (it can be nested inside the object, you would then just reference it with its path):

(2) Add an element to it with Invoke Method activity and that’s it :slight_smile:

1 Like