Assign item from JSON to an array

Hi,

I am using assign activities to set variables from my deserialized JSON such as:
JSON_Deserialised.root(“Secondary_Details”).Item(“premium”).Value(of string)

However, for some fields the results are an array, for example
“length”: [3, 5]
“alternateContactDetailsName”: [“Name”]

And I am unsure how to assign these to array variables.

@SoqedHozi Is it possible to send the xaml file?

Unfortunately not, as the workflow is very large.

Incidentally, I also need to be able to handle empty arrays in this process.

Try assigning to the generic type string @SoqedHozi

Actually, I don’t think we can find whether this is a JSON Object or array. You can use try catch to check whether it is array or object by using both deserialize json and deserialize json array in try and catch respectively

But I don’t want a string, I want an array.

The JSON looks like:
{
“emailId”: “86702008-349437933-1081141457-102265955127082019”,

“secondaryDetails”: {
“premium”: 520.12,
“length”: [3, 5],
“newContactDetailsName”: [“Name”],
“newContactDetailsEmail”: [“Email”],
“newContactDetailsAddress”: [“Address”],
“newContactDetailsPhone”: [“Phone”],
“returnToWorkDate”: [“Date”],
“lossCause”: ,
“product”: “PRODUCT”
}
}

It will retrieve the value like this @SoqedHozi

You can assign it to a string or an array

image

Try the following:

StringArray = DeserialisedJObject(“array”).ToObject(of String())
// to convert “array” to StringArray
{
“array”: [
“value1”,
“value2”
]
}

Cheers

2 Likes

This is just like I am doing already, but without the value setting?

Using JSON_Deserialised.root(“Secondary_Details”).Item(“length”) I get "value of type…jtoken…cannot be converted to ‘1-dimensional array of integer’

What if you don’t know how many records there will be in the array? I can’t follow which variables you are referring to in your answer I’m afraid.

Here it is ,

I’m able to store the value in an array @SoqedHozi

Let me know how you want to store the values . I’m just adding the value to an array at the index 1

image

Like this

StringArray = new String()
StringArray = JSON_Deserialised.root(“secondaryDetails”).Item(“length”).ToObject(of String())

Cheers

image

But I don’t know that the array will contain 3 records as you state, and I cannot see what you’ve used in your second assign activity.

Okay. Just declare “StringArray” as a Array(of String) like you usually do in UiPath

but my array is int32 as shown. Changing your new line to new int32() says I cannot convert integer to 1-dimensional array of integer.

I have declared an array variable “length” as int32

Feel free to change to Int32
Int32Array = JSON_Deserialised.root(“secondaryDetails”).Item(“length”).ToObject(of Int32())

Cheers

I just initialized the array with length 3. You can change it according to your need @SoqedHozi

I appreciate that, but the size is variable from empty to many

My error is occurring in the prior step. Do I leave that as string and only set to int32 in the second assign?

Not clear what error you mean.

You wrote you have an array of Int32.
So this expression “JSON_Deserialised.root(“secondaryDetails”).Item(“length”).ToObject(of Int32())” will give you the array of Int32

The line below just represents variable declaration, you could ignore it.
“StringArray = new String()”