Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More return type for Get Value From JSON #36

Open
pencherek opened this issue May 9, 2022 · 6 comments
Open

More return type for Get Value From JSON #36

pencherek opened this issue May 9, 2022 · 6 comments

Comments

@pencherek
Copy link
Collaborator

I have the idea to add more return type to the keyword Get Value From JSON.
Because what it return is not a JSON, this optonial parameter would let you choose if the keyword must return a json object, a string ?, a array, ...

@elrandira
Copy link
Collaborator

elrandira commented May 9, 2022

It is a bit complex to return an object as the data must be formatted for JSON. I would assume you would like to instantiate an object from the data but the reverse should be applied as well (serialize the object) so you can dump in a JSON file?

as #35 is merged, you can now create a new branch from the master to implement your idea.

@pencherek
Copy link
Collaborator Author

I don't think we can have object in robot framework like in C# or Java, so of do we instantiate a object without class ?

@elrandira
Copy link
Collaborator

You need to create some code in python that could be called by Robot after:

in somescript.py

class somescript:
  @staticmethod
  def json_to_object(self, json):
     return SomeClass(json)

  @staticmethod
  def object_to_json(object):
     return object.doJsonification()

in test.robot

Library JSONLibrary
Library somescript

*** testcases ***
my test
    ${json_obj}=  Load JSON From File  ${some_file_in_json}
    ${my_object}=  json to object  ${json_obj}

    #do something with my_object
    ${new_json}=  object to json  ${my_object}
    Dump Json To File  ${some_file_in_json}  ${new_json}

@elrandira
Copy link
Collaborator

you can call python code with Evaluate:

 ${items} =    Evaluate    [json.loads(item) for item in ('1', '"b"')]    modules=json

or use method on object

  Log to console  ${my_object.saySomething()}

@pencherek
Copy link
Collaborator Author

Ok, just I have no experience in python, I will need to learn

@otemek
Copy link

otemek commented Dec 3, 2022

@pencherek If You could describe more precisely Your case I could try to implement that. For now, the keyword "Get Value From Json" returns a list of found values filtered by expression in a given JSON object:

return [match.value for match in rv]

I think this library doesn't need to operate on custom objects as here we're only playing with the strings, lists, or dictionaries, where at the very end all the values are primitive ones (str, int, bool).
Once again, If You could present some use case scenario then I can try to implement that.
As @elrandira said it will probably require serialization/deserialization implementation for every custom object You want to operate or maybe something very different is on Your mind :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants