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

*** 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 ***


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}
1 Like

Is there a solution to fix the code above?


@EdManlove, can you guide in this regard

Thank you

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.


Thank you
You can check the json structure @

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

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:

    "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?

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()}