How to Parse complex json in Robot framework

1.1k Views Asked by At

I am loading a JSON directly from a file and I need to validate that json for number of attributes . I am using below json file having data arrays and I need to traverse through all the arrays and retrieve the values (The array is dynamic so cant hardcore the loop for 2 times ) How can I get the number of data arrays present in the response so that I can loop through those and get thevalues. Json response :

    {
      "total": 863,
      "data": [
        {
          "id": 6154616,
          "categories": {
            "total": 0,
            "data": []
          },
          "isAnonymized": false,
          "jobOrders": {
            "total": 0,
            "data": []
          },
          "jobSubmissions": {
            "total": 0,
            "data": []
          },
          "lastName": "u engineering",
          "leads": {
            "total": 0,
            "data": []
          },
          "timeZoneOffsetEST": 10
        },
        {
          "id": 85456552,
          "categories": {
            "total": 0,
            "data": []
          },
          "isAnonymized": false,
          "jobOrders": {
            "total": 0,
            "data": []
          },
          "jobSubmissions": {
            "total": 0,
            "data": []
          },
          "lastName": "Engineering",
          "leads": {
            "total": 0,
            "data": []
          },
          "timeZoneOffsetEST": 10
        }
      ]
    }

I have written below Robot code but it is not working.

*** Settings ***
Library    RequestsLibrary    
Library    JSONLibrary 

*** Test Cases ***
check_response        
        ${response_data}=    Load JSON From File    ${response} 
        Log To Console    ${cnf_response}       
        ${no_of_arrays}=    get Length  $.data
        Log To Console  ${no_of_arrays}
       
1

There are 1 best solutions below

0
On

You are using variables not defined in the example - they are probably done so in some other place, but overall that's not how you address a dictionary key in python, e.g. Robot Framework.
Presuming the ${response_data} variable is the parsed json file as a dictionary, this is how to address the key/get the length of the underlying list:

${no_of_arrays}=    Get Length    $response_data['data']