简体   繁体   中英

How to preserve Json Object Order in Robot Framework

I am working with an API that requires the json key value pairs to be ordered when creating resources. The API provides a method (called new) that allows you to make a GET request that will return an object model. I would like to update the model with values within my RobotFramework test cases. Is there a native way within Robot Framework to make a GET request and preserve the json object order sent by the server? Here's an example of the JSON response to the GET new method:

{
"account": {
    "@id": "",
    "@uri": "",
    "@oldID": "",
    "person": {
        "@id": "",
        "@uri": ""
    },
    "accountType": {
        "@id": "",
        "@uri": "",
        "name": null
    },
    "accountName": "",
    "createdDate": null,
    "createdByPerson": {
        "@id": "",
        "@uri": ""
    },
    "lastUpdatedDate": null,
    "lastUpdatedByPerson": {
        "@id": "",
        "@uri": ""
    }
}

}

If I use the following, the key values automatically get sorted:

${r}=   GET Request   MySession   /accounts/new

For anyone else who needs ordered JSON in robot framework, I was able to achieve it with the following (thanks to help in the comments):

&{object}=  Evaluate  json.loads('''${r.text}''', object_pairs_hook=collections.OrderedDict)  modules=json, collections

Though, ultimately this kind of logic is probably best suited in a custom helper library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM