REST API JSON Array with a extended Format

Hey

I read a little bit about my new REST API project and tried to read out the data. Unfortunately, I can’t get any further to read out my data in a structured manner. My REST API is rendered as JSON format. So far this works so well with the “Deserialize JSON” function, but unfortunately not with “Deserialize JSON Array”. I suspect that it is due to the JSON format because it is structured differently. Maybe you can help me how I can read out individual blocks.

For me it would be important to be able to read email, firstname, middlename and lastname from this structure. Do you have any tips?

Here is the JSON format:

{
“data”: [
{
“id”: 0,
“user”: {
“access_group”: {
“id”: 0,
“name”: “string”
},
“id”: 0,
“email”: “string”,
“firstname”: “string”,
“middlename”: “string”,
“lastname”: “string”,
“phone”: “string”,
“gender”: “string”,
},
“team_memberships”: [
{
“team”: {
“id”: 0,
“name”: “string”
}
}
]
}
],
“links”: {
“self”: “string”,
“first”: “string”,
“prev”: “string”,
“next”: “string”,
“last”: “string”
}
}

Understand the structure of your JSON.

The initial JSON string is a JSON object. Inside the JSON object you have a JOSN array under data element.

Inside the data element, you have a JSON Object and email is under the element user as part of a JSON Object.

Please have a look at this article.

1 Like

@Crusha

Please find the attched xaml and replace the message key you will get your all the details
json1.xaml (6.3 KB)

1 Like

Hi @Crusha,

As @KannanSuresh suggested, it is very important to understand the structure of Json especially when you are interfacing / consuming an external API . If I could convey something to the API developer I would have asked them to avoid duplicating multiple keys in the response with the same name (“id” in this example occurs in multiple Objects which is quite odd for an API response). But I understand you have to work with what the API returns to you so this is the flow I suggest.

  1. Get the API response

  2. Convert / Deserialize the API response variable by Deserialize Json activity

  3. Look at the structure of the Json being returned if you find Json objects they can be accessed in UiPath by using Json("JsonObject")

  4. If you want to access Json arrays and the keys with those arrays there is an easier way than using a for loop. Json("JsonObject").ElementAt(INDEXYOUAREINTERESTEDIN) for example Json("JsonObject").ElementAt(0) will return the 0th element in the array and so on.

  5. In your case the User information is buried within a JsonObject which is within a Jsonarray
    So you can access the firstname and lastname values by
    JsonObject("data").ElementAt(0)("user")("firstname").ToString
    JsonObject("data").ElementAt(0)("user")("lastname").ToString

    JsonArray.ElementAt(n) is pretty handy method to access Json array objects.

  6. If you have multiple arrays containing the user information you can then use a For Each Loop go to the Properties create an Index variable and use that Index variable to get all user information
    JsonObject("data").ElementAt(IndexVariable)("user")("firstname").ToString
    JsonObject("data").ElementAt(IndexVariable)("user")("lastname").ToString

Starter code
You can find the starter code .xaml file and your .json input file here:
AccessingJsonObjectsFromJsonArray.zip (2.1 KB)

Further practice
You could practice accessing Json objects from the data in this thread as well: Iterating through Jobject

Hope this clears things a bit.

1 Like

Wow thank you so much!

@KannanSuresh your link is very helpful! I will read it through

@Divyanshu_Divyanshu i have tested your script but i get a Error: “Message Box: Object reference not set to an instance of an object.”

@jeevith thx you. I will test your method

@Crusha May you please check your json file it have all the necessary quotes and braces because for me it is working fine and there was an extra curly bracket somewhere i removed that

Hello,
In these videos, I have multiple examples with JSON creating and deserialization:

Thanks,
Cristian