0

Currently I want to run a for loop. However I don't know what is the limit of my list. So to find that I want to count number of occurrences of a specific KEY in this case accountId which is a list in JSON format. How to count occurances of KEY in JSON data?

{"cardAccountList":[
{
"accountId": "1234",
},
{
"accountId": "1111"
}]} 
martineau
  • 119,623
  • 25
  • 170
  • 301
Jacob
  • 101
  • 2
  • 5
  • 11
  • Please share minimum amount of code, the input and the expected output that are necessary for a [MCVE]. – timgeb Oct 31 '18 at 10:42
  • Please provide a shortened demo data, your approach and a "what I get" and "what I should get" with reasonings. – Patrick Artner Oct 31 '18 at 10:43
  • And how does RobotFramework related to this question? I guess, this is "pure" python question. – kosist Oct 31 '18 at 10:45
  • @kosist this is not a python question at all. My requirement is purely in Robot framework. So clear that confusion first – Jacob Oct 31 '18 at 10:58
  • Possible duplicate of [How to find a particular json value by key?](https://stackoverflow.com/questions/14048948/how-to-find-a-particular-json-value-by-key) – martineau Oct 31 '18 at 11:06
  • @martineau I do not want to use Python. I need to use Robot framework only :) – Jacob Oct 31 '18 at 11:17
  • 1
    sangram: Since the [Robot framework](https://en.wikipedia.org/wiki/Robot_Framework) is written using the Python programming language, the linked question seemed like a match. It's also the best language to implement an add-on in (according to the linked article) if that matters. – martineau Oct 31 '18 at 11:33
  • @martineau I completely agree RF is written in Python, but i am wondering is it really possible to use Python functionalities or inbuilt function in Robot framework. I didn't find any tutorial or library where we can use Python script in robot scripts. – Jacob Oct 31 '18 at 12:10

1 Answers1

1

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
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43