1

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
pgtips
  • 1,328
  • 6
  • 24
  • 43
  • You, can't rely on any order of keys in dictionary. The order isn't even specified in such structure - I.e. there isn't any order. And from this perspective, json isn't anything more than nested structure of dictionaries and lists. See e.g. this: http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv – Jan Kovařík Apr 19 '17 at 05:28
  • @JanKovařík in general you are correct, but RF is a special case - the dicts there are based off [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict), so the insertion order is preserved. – Todor Minakov Apr 19 '17 at 11:27
  • @pgtips - you're using the RequestsLibrary, right? I don't see anything in it, which would reorder the response keys... Are you sure it's not the server that's putting them in unexpected order? Firing the same request manually generates a different structure than the one in RF? – Todor Minakov Apr 19 '17 at 11:28
  • @Todor thanks for explanation. – Jan Kovařík Apr 19 '17 at 11:33
  • @JanKovařík, I'm using ExtendedRequestsLibrary as it's an OAuth2 API. I'm 100% certain it's not the server. – pgtips Apr 19 '17 at 11:56
  • @Todor - yes, I am able to make the same request using the Chrome Dev Client and I get the json response above. When I make the request in RF and log ${r}.json() or ${r}.text the key values are re-ordered. – pgtips Apr 19 '17 at 12:25
  • 1
    OK, I don't have neither the lib, nor a proper test system to check myself, but yet, it all comes down from the underlying requests lib - [see here, please](http://stackoverflow.com/questions/35042216/requests-module-return-json-with-items-unordered). There's a solution in the answer you might want to try - e.g. `${r}.json(object_pairs_hook=OrderedDict)`, yet I myself cannot tell will it be propagated down to the requests lib properly (by a fast glimpse of the ExtendedRequestsLibrary, it should...) – Todor Minakov Apr 19 '17 at 12:51

1 Answers1

1

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.

pgtips
  • 1,328
  • 6
  • 24
  • 43