# How to sort objects (users) ascendingly in json array using robot framework

**URL:** <https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183>\
**Category:** Robot Framework\
**Created:** [1 February 2022 02:46 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183 "2022-02-01T02:46:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![muhammadaahsan1](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/muhammadaahsan1/32/1251_2.png) [@muhammadaahsan1](https://forum.robotframework.org/u/muhammadaahsan1)\
**Post date:** [1 February 2022 02:46 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183/1 "2022-02-01T02:46:03Z")

</div>

\*\*\* Settings \*\*\*

Documentation A script that uses the git API to fetch and print the information

Library SeleniumLibrary

Library RequestsLibrary

Library Collections

Library HttpLibrary.HTTP

Library OperatingSystem

#Resource C:/Users/Gillani Family/PycharmProjects/robotfw1/Tests/resources/anyapi

# Test Teardown Close Browser

\*\*\* Variables \*\*\*

${base\_url} [https://api.github.com/](https://api.github.com/)

```
create session Get_Repositories ${base_url}             
${response_users} = get request Get_Repositories /users
#log to console ${response_users.content}
${json_users} = evaluate json.loads('''${response_users.content}''') json 
${Sorted_Users} = Sort List ${json_users}
log to console ${Sorted_Users}

```

---

<div class="post-metadata">

**Author:** ![muhammadaahsan1](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/muhammadaahsan1/32/1251_2.png) [@muhammadaahsan1](https://forum.robotframework.org/u/muhammadaahsan1)\
**Post date:** [1 February 2022 03:13 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183/2 "2022-02-01T03:13:59Z")

</div>

Is there a solution to fix the code above?

Thanks

---

<div class="post-metadata">

**Author:** ![muhammadaahsan1](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/muhammadaahsan1/32/1251_2.png) [@muhammadaahsan1](https://forum.robotframework.org/u/muhammadaahsan1)\
**Post date:** [1 February 2022 14:22 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183/3 "2022-02-01T14:22:08Z")

</div>

@EdManlove, can you guide in this regard

Thank you

---

<div class="post-metadata">

**Author:** ![robinmackaij](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/robinmackaij/32/126_2.png) [@robinmackaij](https://forum.robotframework.org/u/robinmackaij)\
**Post date:** [2 February 2022 17:42 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183/4 "2022-02-02T17:42:07Z")

</div>

Without knowing the structure of your json it’s impossible to answer your question. The root of a json payload is either an array (list) or an object (dictionary). Whether or not `Sort List` at the root level is applicable depends on the json structure. Even if it’s a list, odds are that you’ll need to provide a sorting key to the sort operation, since it’s likely that the list contains objects representing the individual users. You probably want to sort based on some property of that user object.

---

<div class="post-metadata">

**Author:** ![muhammadaahsan1](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/muhammadaahsan1/32/1251_2.png) [@muhammadaahsan1](https://forum.robotframework.org/u/muhammadaahsan1)\
**Post date:** [3 February 2022 04:05 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183/5 "2022-02-03T04:05:24Z")

</div>

Thank you  
You can check the json structure @ [https://api.github.com/search/repositories?q=seleniumhq](https://api.github.com/search/repositories?q=seleniumhq)

I first converted the json string to python objec(i.e in dictionary) by using evaluate and then did operations on it

1.1. Search repositories using the query Seleniumhq  
create session Get\_Repositories ${base\_url}  
${response\_rep} = get request Get\_Repositories /search/repositories?q=seleniumhq  
${json\_rep} = evaluate json.loads(’’’${response\_rep.content}’’’)  
log ${json\_rep}  
set global variable ${json\_rep}  
1.2. Print total\_count  
Log to console ${json\_rep[‘total\_count’]}

I have done it, Is it the right way I am analyzing json here?

Thank you

---

<div class="post-metadata">

**Author:** ![robinmackaij](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/robinmackaij/32/126_2.png) [@robinmackaij](https://forum.robotframework.org/u/robinmackaij)\
**Post date:** [3 February 2022 09:02 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183/6 "2022-02-03T09:02:56Z")

</div>

Well, personally I would put this type of json / dict / list handling in a small custom Python library since you can use native Python methods to work with the dictionary.

There’s some shortcuts you could take…`${json_rep}= ${response_rep.json()}` should also work.

As for the sorting, what you’re sorting in your initial example is this:

```python
{
    "total_count" : 97,
    "incomplete_results: false,
    "items": [<list with 97 objects>]
}

```

I’m pretty sure that is not what you’re looking to sort. You probably want to get the `items` list from the dict and sort that. But those items are dicts with more than 100 properties. So what property do you want to sort them on?

---

<div class="post-metadata">

**Author:** ![muhammadaahsan1](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/muhammadaahsan1/32/1251_2.png) [@muhammadaahsan1](https://forum.robotframework.org/u/muhammadaahsan1)\
**Post date:** [3 February 2022 09:20 UTC](https://forum.robotframework.org/t/how-to-sort-objects-users-ascendingly-in-json-array-using-robot-framework/3183/7 "2022-02-03T09:20:07Z")

</div>

Sorry I put the wrong code, actually i had to automate API using query of sort only, so it was just this;

create session Get\_Users ${base\_url}  
${response\_users} = get request Get\_Users /search/users q=seleniumhq&order=asc&sort=followers  
${json\_users} = evaluate json.loads(’’’${response\_users.content}’’’) json  
log ${json\_users}

Thank you so much for the shortcut; ${json\_rep}= ${response\_rep.json()}
