How to get a required parameter from JSON String using robotframework keyword

可紊 提交于 2020-01-11 13:21:48

问题


The following is my JSON response string

{
 "macKey": "This_is_a_Test_QED_MAC_Key_Which_Needs_to_be_at_Least_32_Bytes_Long",
"subject": "delivery@token.com",
"audience": "qed:test",
"expirationTime": "2016-03-09T23:07:52Z",
"notBeforeTime": "2016-03-09T23:07:52Z",
"jwtId": "",
"permissions": [
"MANAGE_SYSTEM"
],
"useCompactPermissions": false,
"url": "http://172.30.2.155:8080/",
"generatedToken": "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0Iiwic3ViIjoiZGVsaXZlcnlAdG9rZW4uY29tIiwiYXVkIjoicWVkOnRlc3QiLCJxZWRwIjpbIm1hbmFnZVN5c3RlbSJdLCJleHAiOjE0NTc1NjQ4NzJ9.8KlDoK-t6HoOth3na2x6drEAFfXCGLKLt3aAMGg0r0U",
"qeda": null,
"qedp": [
"manageSystem"
],
"issueTime": "2016-03-09T09:34:40Z"
}

From the above one I need to take generatedToken value.


回答1:


Using "To JSON" from RequestsLibrary

If you are using RequestsLibrary to get the JSON data, you can use the To JSON keyword to convert it to an object. You can then use extended variable syntax to access the individual fields.

For example:

| | ${json}= | To JSON | ${response}
| | log to console | \n the token is ${json["generatedToken"]}

Using "Evaluate" from the BuiltIn library

You can use the Evaluate keyword to run the standard python function json.loads to convert a json string to an object. As long as the data doesn't have any triple-quoted strings, this will work:

| | ${json}= | Evaluate | json.loads('''${response}'''} | json
| | log to console | \n the token is ${json["generatedToken"]}



回答2:


You can use robotframework-httplibrary then you can get it like this:

${value}= Get Json Value ${json} /generatedToken




回答3:


${Ref_Id_Value}    Get Json Value    ${POSTResp.content}    /doInitiateFundsTransferResponseBody/transactionInformation/transactionReferenceNumber

You can use the Get Json Value method, I think.



来源:https://stackoverflow.com/questions/35891061/how-to-get-a-required-parameter-from-json-string-using-robotframework-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!