I have a Json file like this.PFB the code:
"properties " : {
"xyz" : {
"username" : "abc@gmail.com",
"password" : "abc@123",
"phonenumber" : "1235",
},
"ABC" : {
"username" : "abc@gmail.com",
"password" : "abc@123",
"phonenumber" : "1345",
},
Keyword will be like :
Do operation for properties
[Arguments] ${username} ${password} ${phonenumber}
Log ${username}
Log ${password}
Log ${phonenumber}
My questions are :
1) The json file contains so many things but i have to fetch only properties from the file.How i will take properties part from the entire json file and pass directly arguments like username , password , phonenumber into keyword mentioned above.
2) How to write keyword for this logic such that we change only json file for adding more properties like apart from xyz, abc we will add as many properties we want and it will automatically fetch and keyword give us the desired result for all properties whatever we are modifying in json file.
If I understand you correctly, your question's summary is: a) how to read and parse a json file in Robotframework in this form, and b) pass each record's attributes to this keyword.
A file can be read from the file system with Get File
.
One can read a json file with the python's json
module, more specifically the loads()
method - it takes a string, and returns a python object.
Your "json" sample is quite invalid json, so let's imagine that "properties" is somewhere (3 levels deep) inside the file.
${the file as string}= Get File c:\\the\\path\\to\\the\\file.json
${parsed}= Evaluate json.loads("""${the file as string}""") json
${properties}= Set Variable ${parsed["top"]["child"]["properties"]}
And now the variable properties
is a dictionary, with those two keys - "ABC" and "xyz"; you just iterate over it, and pass the subkeys of each of the sub-dictionaries to the keyword.
FOR ${key} IN @{properties}
${sub dict}= Get From Dictionary ${properties} ${key}
Do operation for properties ${sub dict}[username] ${sub dict}[password] ${sub dict}[phonenumber]
END
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.