How to Updating Value in the Nested Dictionary?

2,412 views
Skip to first unread message

Chia-Wei Chang

unread,
Jun 28, 2017, 2:27:11 AM6/28/17
to robotframework-users
Hi everyone,

I want to Use GET and PUT to access this json file using robot framework.

{
"A": {
   
"B": [
       
{
           
"C00": 0,
           
"D00": "test00"
       
},
       
{
           
"C01": 0,
           
"D01": "test01"
       
}
         
]
   
}
}

After get json file and my code is 
${json} =            Get Response Body
&{data} =            Parse Json              ${json}
Log to console       ${data['A']['B'][0]['C00']}

I was successful to get the value of "C00" , but I do not know how to set nested dictionary value. 
For example ,  Set "C00": 0 to "C00": 1

I have tried "Evaluate". The ${data['A']['B'][0]['C00']}  change the value from 0 to 1,  but nothing happened in the ${data}

${data['A']['B'][0]['C00']} =     Evaluate     1
Log to console                  ${data['A']['B'][0]['C00']}  # C00 = 1
Log to console                  ${data}                      # C00 = 0

then I use "Set to Dictionary".  ${data['A']['B'][0]['C00']} is 0 , but ${data} add a item behind.

Set to Dictionary      ${data}     ${data['A']['B'][0]['C00']}    1
Log to console         ${data['A']['B'][0]['C00']}    # C00 = 0
Log to console         ${data}  

 ${data}
{
"A": {
   
"B": [
       
{
           
"C00": 0,
           
"D00": "test00"
       
},
       
{
           
"C01": 0,
           
"D01": "test01"
       
}
         
]
   
},
1:  1

}

I read Accessing Sub Dictionary Key-Value To Manipulate , but still can't have a way to solve the problem.

Thanks,
William


Kevin O.

unread,
Jun 28, 2017, 12:35:18 PM6/28/17
to robotframework-users
The dictionary you want to manipulate is not ${data}, but one nested inside.
Try this:

Set to Dictionary      ${data['A']['B'][0]}    C00=${1}


Cheers,
Kevin

The $ and curly braces around 1 are to get 1 as an integer. Since that is the type that is already present, it seems more appropriate.

Pekka Klärck

unread,
Jun 28, 2017, 3:25:33 PM6/28/17
to iwt...@gmail.com, robotframework-users
Accidentally replied only to sender earlier.


28.6.2017 12.42 ip. "Pekka Klärck" <pe...@iki.fi> kirjoitti:
Hello,

I think you should be able to use something like

    Set To Dictionary    ${data['A']['B'][0]}    C00    1

Notice that when you use


    ${data['A']['B'][0]['C00']} =     Evaluate     1

what you actually get is variable with name
${data['A']['B'][0]['C00']}. The only assignment syntax that is
supported in addition to the normal

    ${name} =    Keyword    args

is

    ${name.attribute} =    Keyword    args

Adding support for what you used would be possible, but perhaps a bit
risky considering backwards compatibility. It would be safer to
support

    ${data}[A]'B][0][C00] =     Keyword    args

similarly as https://github.com/robotframework/robotframework/issues/2601
proposes.

In general working with (nested) dicts and lists should be made easier
in Robot. This can include syntax changes and also enhancements to
BuiltIn and Collections libraries. Enhancement ideas and general
comments about the matter are appreciated!

Cheers,
    .peke
> --
> You received this message because you are subscribed to the Google Groups
> "robotframework-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to robotframework-users+unsub...@googlegroups.com.
> To post to this group, send email to robotframework-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/robotframework-users.
> For more options, visit https://groups.google.com/d/optout.



--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Message has been deleted

Chia-Wei Chang

unread,
Jun 29, 2017, 10:01:38 PM6/29/17
to robotframework-users, iwt...@gmail.com
Hi, Pekka and Kevin

thank you, It solved my problem.



Reply all
Reply to author
Forward
0 new messages