Create disctionary from string

4,911 views
Skip to first unread message

CLAYTON

unread,
May 12, 2016, 5:48:54 AM5/12/16
to robotframework-users
Hello.

Is there any built in keyword that will allow to create a dictionary variable from say a json string?

e.g. to convert a string such as:

"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter","lastName":"Jones"}
]

Into a Robot framework dictionary variable

And vice versa --> convert a robot dictionary to a string.

If not i think such keywords would be really handy.
Not just for API testing (where json is used alot), but also for the .Net Remote Keyword server, where converting robot framework dictionaries to .net dictionaries is quite difficult
(As the moment NRobotRemote doesnt support dictionaries)
The problem i face with .net is the RF dictionaries come through as xml-rpc structures, which then can only get de-serialised to concrete c# structures with matching key names

Many thanks.




Bryan Oakley

unread,
May 12, 2016, 7:18:13 AM5/12/16
to clayto...@yahoo.co.uk, robotframework-users
This stackoverflow answer gives and example of converting data from and back to JSON: http://stackoverflow.com/questions/35262216/json-handling-in-robot/35272804#35272804


--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Pekka Klärck

unread,
May 12, 2016, 9:46:04 AM5/12/16
to Bryan Oakley, Clayton Neal, robotframework-users
2016-05-12 14:18 GMT+03:00 Bryan Oakley <bryan....@gmail.com>:
> This stackoverflow answer gives and example of converting data from and back
> to JSON:
> http://stackoverflow.com/questions/35262216/json-handling-in-robot/35272804#35272804

That's a great answer. In simple cases you don't even need to use the
json module with Evaluate keyword because simple JSON is also valid
Python.

I wonder would it make sense to create a separate library for handing
JSON data. One alternative is adding keywords to load and dump JSON to
Collections and use functionality it already provides for
manipulation. Could obviously add similar YAML keywords there too.

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

CLAYTON

unread,
May 12, 2016, 11:01:18 AM5/12/16
to robotframework-users, bryan....@gmail.com, clayto...@yahoo.co.uk
Thanks for the answer.

For the xml-rpc representation of a dictionary, that is where my problem really is
Ideally id like robot dictionaries sent to remote library serialised as a string.. the remote library can de-serialise it to its corresponding type
Same for dictionary return values from a remote library keyword, returned to robot as a string and robot de-serialises them into a dictionary
As remote libraries can be in different programming languages, not all can represent a robot dictionary (xml-rpc struct) in their language built in types

So if there was a built in keyword to convert a dictionary to json, and vice-versa it would be good as json is more language independant that xml-rpc structures.

CLAYTON

unread,
May 12, 2016, 11:04:09 AM5/12/16
to robotframework-users, bryan....@gmail.com, clayto...@yahoo.co.uk
Even a new operator could be good

e.g. if ${DICT} is a dictionary variable then, #{DICT} could be its json string

Pekka Klärck

unread,
May 12, 2016, 12:17:38 PM5/12/16
to Clayton Neal, robotframework-users, Bryan Oakley
2016-05-12 18:01 GMT+03:00 'CLAYTON' via robotframework-users
<robotframe...@googlegroups.com>:
>
> For the xml-rpc representation of a dictionary, that is where my problem
> really is
> Ideally id like robot dictionaries sent to remote library serialised as a
> string.. the remote library can de-serialise it to its corresponding type
> Same for dictionary return values from a remote library keyword, returned to
> robot as a string and robot de-serialises them into a dictionary
> As remote libraries can be in different programming languages, not all can
> represent a robot dictionary (xml-rpc struct) in their language built in
> types

Do you have examples of languages supporting XML-RPC that don't
support its struct data type? Python implementation converts them to
dict, Java to java.util.Map, etc.

> So if there was a built in keyword to convert a dictionary to json, and
> vice-versa it would be good as json is more language independant that
> xml-rpc structures.

Keywords for loading and dumping JSON would be useful also in many
other cases. It's obviously trivial to implement them in a custom
library, but I'd be fine adding them to Collections. Or would there be
more related keywords that JSON should get its own library?

CLAYTON

unread,
May 12, 2016, 12:49:42 PM5/12/16
to robotframework-users, clayto...@yahoo.co.uk, bryan....@gmail.com
Im always the odd one out using .net for remote libraries :-)
For .net the most common xml-rpc library in use doesn't de-serialise xml-rpc structs to .net Dictionary types but instead de-serialises them to c# struct types
This means they are not dynamic. The c# structure has to match the xml-rpc structure
In Java de-serialising to a Map makes it dynamic

See : http://xml-rpc.net/faq/xmlrpcnetfaq-2-5-0.html#1.10

Hence why if i could dictionaries get back/forth to .net as json strings id avoid that problem, as i could de-serialise the string myself (outside of above library)

As for a new JSON library... sure why not, i guess as a new standard library would mean could be added to in the future
For me only requirement would be

Convert To Json <variable>   -- returns a string
Create From Json <string>   -- returns a dict

I know HAR files as json, as are most API data, so for web/api testers their could be some interesting data related keywords

Many thanks for help.

Pekka Klärck

unread,
May 12, 2016, 2:30:30 PM5/12/16
to Clayton Neal, robotframework-users, Bryan Oakley
2016-05-12 19:49 GMT+03:00 'CLAYTON' via robotframework-users
<robotframe...@googlegroups.com>:
> Im always the odd one out using .net for remote libraries :-)
> For .net the most common xml-rpc library in use doesn't de-serialise xml-rpc
> structs to .net Dictionary types but instead de-serialises them to c# struct
> types

C# has a dictionary type, right? An XML-RPC module not using it sounds
like a _really_ strange design decision. Have you looked are there
other .NET XML-RPC modules that would work more sanely in this regard?

> Hence why if i could dictionaries get back/forth to .net as json strings id
> avoid that problem, as i could de-serialise the string myself (outside of
> above library)

I see. You obviously can implement custom solutions that mitigate this
problem, but I don't want to change Remote interface itself to
workaround problems caused by some XML-RPC implementation.

> As for a new JSON library... sure why not, i guess as a new standard library
> would mean could be added to in the future
> For me only requirement would be
>
> Convert To Json <variable> -- returns a string
> Create From Json <string> -- returns a dict

If the library would only have these keywords, I think it would be
better to just add them to Collections. Its keywords would most likely
be used to manipulate the JSON structure. Separate library for JSON
would make sense only if there would be more JSON only related
keywords.
Reply all
Reply to author
Forward
0 new messages