Extracting values from JSON response

5.208 visualitzacions
Ves al primer missatge no llegit

Tejas Kashyap

no llegida,
13 de juny 2017, 18:23:3713/6/17
a robotframework-users
Hi All,

I am trying to extract the value for a particular parameter that is available in JSON format. I am using the RequestsLibrary to do the same. I am attaching the robot code below. I get the response code as 200 but unable to parse the values obtained. I get the error "AttributeError: 'Response' object has no attribute 'values' "

In the below example, I want to get the value for the field "mac-address".

Have attached the corresponding curl output as well.

*** Settings ***
Library           Selenium2Library
Library           RequestsLibrary
Library           Collections

*** Test Cases ***
confd
    ${headers} =    Create Dictionary    'accept'    "application/vnd.yang.data+json"
    ${auth}=    Create List    admin    admin
    Create Session    test    http://192.168.1.10:8008    headers=${headers}    auth=${auth}
    ${resp}=    Get Request    test    /api/running/managed-ap    headers=${headers}
    Should Be Equal As Strings    ${resp.status_code}    200
    Dictionary Should Contain Value    ${resp}    02:42:C0:A8:01:0F


-----------------------------------------------------------------------------------------------------------------------------------------------------------

curl output:

sudo curl -v -u "admin:admin" "http://192.168.1.10:8008/api/running/managed-ap" -H "Accept: application/vnd.yang.data+json"

* About to connect() to 192.168.1.10 port 8008 (#0)
*   Trying 192.168.1.10...
* Connected to 192.168.1.10 (192.168.1.10) port 8008 (#0)
* Server auth using Basic with user 'admin'
> GET /api/running/managed-ap HTTP/1.1
> Authorization: Basic YWRtaYWRtbW4=
> User-Agent: curl/7.29.0
> Accept: application/vnd.yang.data+json
< HTTP/1.1 200 OK
< Server: 
< Date: Tue, 13 Jun 2017 18:23:37 GMT
< Last-Modified: Fri, 01 Jan 1971 01:00:00 GMT
< Cache-Control: private, no-cache, must-revalidate, proxy-revalidate
< Etag: 1488-539402-609801
< Content-Type: application/vnd.yang.data+json
< Transfer-Encoding: chunked
< Pragma: no-cache
{
  "wireless_config:managed-ap": {
    "database": [
      {
        "mac-address": "02:42:C0:A8:01:03"
      }
    ]
  }
}
* Connection #0 to host 192.168.1.10 left intact


Mikhail Goloulnikov

no llegida,
14 de juny 2017, 5:59:0014/6/17
a robotframework-users
How about  to convert to string the body content first?

In my case I use the following

${body}=      Get Request              local           #URL#
${string}= Convert To String ${body.content}

Kevin O.

no llegida,
22 de juny 2017, 0:57:1922/6/17
a robotframework-users
The response object has a json method you can use to convert the response body to a data structure, which if the JSON is an object and not an array or some other type, will be a dictionary.

    ${resp}=    Get Request    test    /api/running/managed-ap    headers=${headers}
    Should Be Equal As Strings    ${resp.status_code}    200
    ${dict}    Set Variable    ${resp.json()}
    Dictionary Should Contain Value    ${dict}    02:42:C0:A8:01:0F

or just directly...
    Dictionary Should Contain Value    ${resp.json()}    02:42:C0:A8:01:0F
Respon a tots
Respon a l'autor
Reenvia
0 missatges nous