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.
-
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 exampleJson("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 thatIndex
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.