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?
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.
Get the API response
Convert / Deserialize the API response variable by Deserialize Json activity
Look at the structure of the Json being returned if you find Json objects they can be accessed in UiPath by using Json("JsonObject")
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.
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.
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
@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