简体   繁体   中英

How to update/replace value in the dictionary/json file where the value of the dictionary's key type is list using robot framework

I am trying to update the value "Four" to "four"

json format:

"Num" : {
        "Roman": [
        {
            "word_rep": {
                "IV": [
                    "Four"
                ]
            }
        }
        ]
    }

code:

${file}    Get File    file1.json
${json_file}    Evaluate    json.loads('''${file}''')    json
${new_val}   set variable   four
set to dictionary      ${json_file["Num"]['Roman'][0]['word_rep']}        IV[0]=${new_val}

When I try the above code i am getting a new dictionary added({'IV[0]':'four'}) to the existing dictionary I want to replace the value and not new addition.

Can anyone help me out here.

Thanks in advance

The issue is that you try to use a dictionary keyword to update a list.
Ie Robot Framework will treat IV[0] as a string, and since the key don't exist it will set it to the dictionary as a new entry.

To update the list you need to refer to it as a list with Set List Value .
Below should work accordingly.
Set List Value ${json_file["Num"]['Roman'][0]['word_rep']['IV']} 0 ${new_val}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM