0

For Robotframework, in given testcase code 1 and code 2 to access dictionary object. Problem is that when I use json.load to convert my json object which returns a list, returns json keys in single ' instead of double comma " object and when i don't use json.load it returns Unicode error

define library

*** Settings ***
Library  OperatingSystem
Library  Collections
Library  HttpLibrary.HTTP

*** Test Cases ***
Code1
    #get json file
    ${json_data}=    Get file  detail.json

    #get  dictionaries under list
    ${valuelist}=  Get Json Value    ${json_data}    /alladdress/addresslist

    # display it
    log to console  ${valuelist}

    # loop over  dictionaries under list
    : FOR  ${key}   in   @{valuelist.keys()}
    \  ${value}=    Get From Dictionary    ${valuelist}    ${key}

    # getting AttributeError: 'unicode' object has no attribute 'keys
    \  log to console   ${key},${value}


Code2
    # get json file
    ${json_data}=    Get file  detail.json

    # get  dictionaries under list
    ${valuelist}=  Get Json Value    ${json_data}    /alladdress/addresslist

    # use below line to avoid unicode error
    ${obj_list}=  evaluate    json.loads('''${valuelist}''')    json

    # display it
    log to console  ${obj_list}
    # loop over  dictionaries under list

    : FOR  ${key}   in   @{obj_list.keys()}
    \  ${value}=    Get From Dictionary    ${obj_list}    ${key}

    # getting AttributeError: 'list' object has no attribute 'keys'
    \  log to console   ${key},${value}

here is json file

{
   "class":{
      "id":0,
      "name":"David"
   },
   "alladdress":{
      "count":3,
      "addresslist":[
         {
            "houseno":1,
            "streetno":5,
            "streetname":"tesla",
            "city":"ABC",
            "state":"AA",
            "country":"UK",
            "zip":85555
         },
         {
            "houseno":2,
            "streetno":6,
            "streetname":"honda",
            "city":"PQR",
            "state":"BB",
            "country":"IN",
            "zip":5252
         }
      ]
   }
}

2 Answers 2

1

In the HttpLibrary Library there is the Parse JSON keyword that is of use here. It can convert the string of the JSON document that is fetched using Get JSON Value into a dictionary.

So the value here is that you don't have to 'walk' the dictionary to get to the node you're looking for.

*** Settings ***
Library  OperatingSystem
Library  HttpLibrary.HTTP

*** Test Cases ***

Fetch Address List
    ${json_data}=    Get file  details.json

    ${addressesJSONstring}    Get Json Value    ${json_data}    /alladdress/addresslist
    ${addresseslist}          Parse Json        ${addressesJSONstring}

    : FOR  ${addressDict}   in   @{addresseslist}
    \  log   ${addressDict['country']}
Sign up to request clarification or add additional context in comments.

2 Comments

this code gives error Importing test library 'HttpLibrary.HTTP' failed: ImportError: No module named HttpLibrary
Can you verify using pip list and verify that robotframework-httplibrary is in this list? If not, then installing it using pip install robotframework-httplibrary solves that.
0

It appears that the keyword Get json value is returning strings rather than objects. If you replace that call with code that uses python's json module, you can then parse the data to find what you want.

For example, this will print out each address dictionary:

*** Test Cases ***
Code1
    #get json file
    ${json_data}=    Get file  detail.json

    #get  dictionaries under list
    ${data}=  evaluate  json.loads($json_data)  json
    ${alladdress}=  get from dictionary  ${data}  alladdress
    ${addresslist}=  get from dictionary  ${alladdress}  addresslist

    # loop over  dictionaries under list
    log to console  addresses:
    : FOR  ${address}  in  @{addresslist}
    \   log to console  ${address}

5 Comments

@Bryan.Thanks of reply. i replaced it by $ but still i get same error (unicode)
i am looking to get response from /alladdress/addresslist , so i have to use ${valuelist}= Get Json Value ${json_data} /alladdress/addresslist to parse it and then ${valuelist} contain list which have dictionary element inside. In your answer you have used entire json file.
@madhur: ok, I've tried to make it a bit more clear. The bottom line is that the library you're using to parse the JSON isn't returning actual objects.
is there nay way to access nested element in json which return list with dictionary.Like in your example it is entire json file
@madhur: you have to read in the whole json file. Your original code was doing that whether you realize it or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.