Norman Kuo
Norman Kuo

Reputation: 133

How to loop dictionary within dictionary

I am trying to loop through dictionary inside a dictionary, the main idea is to check of the value is a path, right now I am just printing the value with (Log to Console) and figuring out rest of the for loop, but I am getting stuck on the second loop

    FOR   ${key}   IN   @{dict.keys()}

    ${value}=    Get From Dictionary    ${dict}    ${key}
    ${type} =    Evaluate    type($value).__name__
    ${contains}=  Evaluate   "/" in """${value}"""
    Log To Console   ${type}

    Run Keyword If    ${type} == str and ${contains}    
    ...     Log To Console   ${key}, ${value}
    
    Run Keyword If    ${type} == dict
    ...     FOR   ${key2}   IN   @{value.keys()}
    ...     ${value2}=    Get From Dictionary    ${value2}    ${key2}
    ...     Log To Console   ${key2}, ${value2}
    ...     END
    END

I am getting this error:

Variable '${key2}' not found. Did you mean:
    ${key}

How to I access the value inside the dictionary that is inside the dictionary?

Thank you in advance

Upvotes: 0

Views: 1208

Answers (1)

Todor Minakov
Todor Minakov

Reputation: 20067

The syntax you used with the inner loop is not correct:

    Run Keyword If    ${type} == dict
    ...     FOR   ${key2}   IN   @{value.keys()}
    ...     ${value2}=    Get From Dictionary    ${value2}    ${key2}
    ...     Log To Console   ${key2}, ${value2}
    ...     END

The triple dot is used as a continuation of a keyword call on the next line, while the FOR syntax explicitly expects the block to be on multiple lines. If you change the condition to a regular IF, it should work:

    IF    “${type}“ == “dict“
         FOR   ${key2}   IN   @{value.keys()}
             ${value2}=    Get From Dictionary    ${value2}    ${key2}
             Log To Console   ${key2}, ${value2}
         END
    END

Upvotes: 1

Related Questions