Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing dictionary value from list of dictionaries

I am using a keyword that returns a list of dictionaries like the one below. If I loop through the dictionaries I can get the values. For example,

${vPools} =      list all vpools
: FOR    ${Item}    IN    @{vPools}
\    ${vPoolID} =     ${Item["VPoolID"]}

Is there a way to access a value directly without looping? Something like

${vPoolID} =   @{vPools[0]}["VPoolID"]

for the first list item? I keep trying different things but can't seem to find the right recipe.

17:23:30.815    INFO    [
    {
        "GroupType": 3, 
        "GroupTypeEnum": 3, 
        "Name": "GS - Flash - Protection: 1 - QoS: Gold", 
        "OptimizationSetting": 0, 
        "OptimizationSettingEnum": 0, 
        "PhysicalPoolID": "7efaf334-b723-4be6-af56-3a626562b553", 
        "ProtectionSetting": 1, 
        "QoSClassPriorityID": 1, 
        "Secured": false, 
        "TotalCapacity": 0, 
        "UsedCapacity": 0, 
        "VPoolID": "be7d518e-9851-4ad5-bd59-c7987fc3dd9b"
    }, 
    {
        "GroupType": 3, 
        "GroupTypeEnum": 3, 
        "Name": "GS - Flash - Protection: 1 - QoS: Silver", 
        "OptimizationSetting": 0, 
        "OptimizationSettingEnum": 0, 
        "PhysicalPoolID": "7efaf334-b723-4be6-af56-3a626562b553", 
        "ProtectionSetting": 1, 
        "QoSClassPriorityID": 2, 
        "Secured": false, 
        "TotalCapacity": 0, 
        "UsedCapacity": 0, 
        "VPoolID": "1a82e650-589e-4077-878b-48eea7211278"
    }, 
    {
        "GroupType": 3, 
        "GroupTypeEnum": 3, 
        "Name": "GS - Flash - Protection: 1 - QoS: Bronze", 
        "OptimizationSetting": 0, 
        "OptimizationSettingEnum": 0, 
        "PhysicalPoolID": "7efaf334-b723-4be6-af56-3a626562b553", 
        "ProtectionSetting": 1, 
        "QoSClassPriorityID": 3, 
        "Secured": false, 
        "TotalCapacity": 0, 
        "UsedCapacity": 0, 
        "VPoolID": "5a082095-cbb5-460a-a0fc-0a8dc90d007d"
    }
]()
like image 785
Perry Avatar asked Oct 26 '25 12:10

Perry


2 Answers

Collections library has keyword Get From Dictionary. I think it is what you are looking for:

*** Settings ***
Library    Collections

*** Test Cases ***
Dictionary
    ${dict1}=      Create Dictionary     GroupType=3    VPoolID=be7d518e-9851-4ad5-bd59-c7987fc3dd9b
    ${dict2}=      Create Dictionary     GroupType=3    VPoolID=1a82e650-589e-4077-878b-48eea7211278
    ${vPools}=     Create List           ${dict1}       ${dict2}
    ${vPoolID}=    Get From Dictionary   @{vPools}[0]   VPoolID
like image 56
Pekka Avatar answered Oct 28 '25 02:10

Pekka


Yes you can access nested collections if you use the extended variable syntax. This comes in quite handy when working with DatabaseLibrary and SudsLibrary. Make sure to put the keys and indices inside the curly braces and put your keys in quotations.

List of Dictionaries
    ${a}    Create Dictionary    a=1    b=2
    ${b}    Create Dictionary    x=5    y=7
    ${stuff}    Create List    ${a}    ${b}
    ${item}    Set Variable    ${stuff[0]['b']}
    Should Be Equal    ${stuff[1]['x']}    5

So try:

${vPoolID}    Set Variable    ${vPools[0]["VPoolID"]}

Since you are referencing a single item, do not use @, but use $ instead.

like image 41
ombre42 Avatar answered Oct 28 '25 03:10

ombre42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!