madhur
madhur

Reputation: 173

How to display key and value pairs of dictionary which appears after loop over loop

Code 1 works but its partially hardcoded to get al city information , i am looking to display all key value pair at same time.

*** 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. I wanted to use loop  FOR  ${address}   in   ${addresslist.keys()} but for some reason its not working so i use this code to display key value pair
    : FOR  ${address}  in  @{addresslist}
    \   ${city} =  Get From Dictionary  ${address}   city
    \   ${key}=  set variable  ${address.keys()}
    \   ${listkey}=  get from list  ${key}  0
    # since i know list 0 is city but its kind of hardcoded
    \   log to console  ${listkey}, ${city}
    # i am trying to display key value pair using below code but it displays error @{address.keys()}' failed: Variable '${address}' not found.
Code2
    #get json file
    ${json_data}=    Get file  detail.json
    ${data}=  evaluate  json.loads($json_data)  json
    ${alladdress}=  get from dictionary  ${data}  alladdress
    ${addresslist}=  get from dictionary  ${alladdress}  addresslist
    # loop over list which contents dictionary object.
    :FOR  ${address}  IN  @{addresslist}
    \  Loop over address  @{address}
Loop over items
    [Arguments]  @{address}
    :FOR  ${key}  IN  @{address.keys()}
    \  ${value}=    Get From Dictionary    ${address}    ${key}
    # here i get error @{address.keys()}' failed: Variable '${address}' not 
       found.
    \  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}]}}

Upvotes: 1

Views: 3709

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386010

In the code you have this comment:

# loop over  dictionaries under list. I wanted to use loop  FOR  ${address}
# in ${addresslist.keys()} but for some reason its not working so i use
# this code to display key value pair

The reason :FOR ${address} IN ${addresslist.keys()} doesn't work is because ${addresslist} is a list, not a dictionary. Lists don't have keys.

You need to loop over every address in ${addresslist}, and then within that loop you can call a keyword to print the key/value pairs of each element in the list.

Here is a complete working example:

*** Settings ***
Library  OperatingSystem
Library  Collections

*** Keywords ***
Log dictionary
    [Description]  log key/value pairs from dictionary to the console
    [Arguments]  ${dict}
    log to console  \n----
    :FOR  ${key}  IN  @{dict.keys()}
    \  ${value}=  get from dictionary  ${dict}  ${key}
    \  log to console  ${key} => ${value}

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

    ${data}=  evaluate  json.loads($json_data)  json
    ${alladdress}=  get from dictionary  ${data}  alladdress
    @{addresslist}=  get from dictionary  ${alladdress}  addresslist

    :FOR  ${address}  in  @{addresslist}
    \  log dictionary  ${address}

Upvotes: 1

Related Questions