How to read array from .json file?

@Madhavi

Please find the json file
data.json (690 Bytes)

The http response you are getting is not in the form of array. You have to use Deserialize Json activity and access the elements.
If you have to access each item under Data, use for each loop and specify json(“Data”) in Values property.

Hi All,

@karthik_bethi Thanks for sharing the video.

But still its not working for me. I am getting the below error when I am trying to run the workflow. please find my workflow below.

18.3.1+Branch.master.Sha.4c05f670b311e90ee097c589605b399e9bee4874

Source: Deserialize JSON

Message: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type ‘Newtonsoft.Json.Linq.JToken’ because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path ‘page’, line 1, position 8.

Exception Type: Newtonsoft.Json.JsonSerializationException

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type ‘Newtonsoft.Json.Linq.JToken’ because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path ‘page’, line 1, position 8.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
at UiPath.Web.Activities.DeserializeJson`1.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

APITest.xaml (7.1 KB)

Use deserialised json array activity if your json is array of json object.If your json is singles json object use desialuzed json object activity

@karthik_bethi

I am getting compiler error when using Deserialize JSON Array.

Hey,

Can you try some things? Like try converting the type to another type and show what errors you get…

Do this inside your For each to see what compiler errors you get:

CType(OutputJson, JArray())
should give you this error:
image
Notice it shows the full namespace infront.

CType(OutputJson, JArray)
This should return no compiler errors; if it does show a compiler error, please show the error.

CType(OutputJson, Newtonsoft.Json.Linq.JArray)
This should return no compiler errors also because it contains the full namespace

Basically I’m just converting the JsonArray to the JArray type which is pointless, but maybe it will help identify any issues.

Personally, it seems like the JArray type is not declared correctly in your variables. But maybe it is and it’s just glitching… you can try starting a new workflow file and see if the errors get resolved in the new file.

Regards.

Hi @ClaytonM

Thanks for the support. I started a new workflow as you said and I am still getting the below error.

What is namespace is it TypeArgument? You can find my workflow
(APITest.xaml) file above.SampleAPITest.xaml (1.9 KB)

In your APITest.xaml, I created a JArray and had no compiler issues, so must be on your end:

TypeArgument for the “For each” is what type “item” will be while looping. In this case, it will be either an “Object”, “JToken” or “JObject”, since that will be the type of items the JArray contains.

@ClaytonM Can you please share APITest.xaml file so that I can compare with my workflow and see where I am going wrong.

APITest.xaml (8.8 KB)
Sawwry.

I added the For each too, which had no compiler errors.

2 Likes

Hi @ClaytonM,

Why activity are not loading. Did you had the same issue when you downloaded my APITest.xaml.

Seems as though you have the Web activities installed from a different source maybe.
Check in Manage Packages to make sure you have the right version installed:

I also updated to 1.2 but there were no differences.

Regards.

Also clear your project.json file of dependencies if the previous post didn’t fix it, cause I have seen sometimes it try to load those activities with wrong versions.

In any case, though, you can see the For each with no compiler errors. So if you can’t get the activity working, just move in the activities again and delete the error’d ones.

But, at the same time, I would still suggest making sure your Web activities are installed from the Gallery if they aren’t already :smiley:

Hi @ClaytonM,

Thanks for the support. I have uninstalled “UiPath.Web.Activities” and reinstalled it again and started a new workflow. Now it is working.

Thanks a lot :slight_smile:

:sweat::sweat::sweat: Sanity Restored!

1 Like

Hi Friends,

How to get the total count of MessageId in the below json file.

I tried the below code and every time is giving me '0’

item1(“MessageId”).Count.ToString

Hi @Bhaskar_Mukka

With json you need to simply follow the breadcrumbs of your json structure. In this case, the path to the value you want to count looks like this:
Data -> InvalidMessageList -> MessageId

therefore, if your Json variable is called item1, then the syntax should look somewhat like that:
item1("Data")(0)("InvalidMessageList").Count

The extra zeros are indexes of the arrays, because everything between [] is an array. This also means that it gets trickier to count the MessageId value. But this means that you can either count the number of elements in the InvalidMessageList in general (as posted above) OR use this as an input of a For Each activity and count your value by incrementing some variable.

4 Likes

Thanks @loginerror. It worked :slight_smile:

1 Like