What you want to do can be done in Robot Framework, but if your real world issue is more complex please consider a custom Python keyword. Just because you can, does not always mean you should.
In the below json file I removed the comma after "accountId": "1234"
. It loads the json from a file and then converts it to an object (Dictionary/List/Dictionary) and then cycles through the list to check the sub-dictionary for the existence of the accountId
key. Counts the number of time it finds it.
json.json
{"cardAccountList":[
{
"accountId": "1234"
},
{
"accountId": "1111"
}
]
}
And with the following robot code:
*** Settings ***
Library OperatingSystem
Library Collections
*** Test Cases ***
TC
${json_string} Get File ./json.json
${json_object} evaluate json.loads('''${json_string}''') json
${count} Set Variable ${0}
${key_count} Get Length ${json_object["cardAccountList"]}
:FOR ${item} IN @{json_object["cardAccountList"]}
\ ${status} Run Keyword And Return Status
\ ... Dictionary Should Contain Key ${item} accountId
\
\ ${count} Set Variable If ${status} ${count+1} ${count}
Log To Console \nCount of accountId is: "${count}"
produces the console log:
==============================================================================
TC
Count of accountId is: "2"
| PASS |
------------------------------------------------------------------------------
Folder.Json | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed