0

I have a json response with nested values. I have read the required keys and values from json. What issue I am facing is I am not able to append the particular keys and values inside a json file where i am storing. So, Can anyone help me in this by giving any solutions.

JSON RESPONSE having tags mulitple keys and values LIKE:

 ${API_Output}= {
"data": {
"resources": {
 "edges": [
    {
    "node": {
        "tags": [],
       }
    },
    {          
      "node": {
       "tags": [
          {
            "name": "app",
            "value": "e2e"
          },
          {
            "name": "Cost",
             "value": "qwerty"
          }
      }
    },
    {          
      "node": {
       "tags": [
          {
            "name": "app",
            "value": "e2e"
          },
          {
            "name": "Cost",
             "value": "qwerty"
          },
            {
            "name": "test",
             "value": "qwerty"
          }
      }
    }
    ]
    }
    }
    }

Robot Framework code:

${dict1}=        Set Variable  ${API_Output}
${cnt}=     get length     ${dict1['data']['resources']['edges']}
${edge}=   set variable      ${dict1['data']['resources']['edges']}

run keyword if   ${cnt}==0     set test message    The resources count is Zero(0)
log to console  ${cnt}-count

: FOR    ${item}    IN RANGE   0    ${cnt}
\    ${readName}=    Set Variable     ${edge[${item}]['node']['configuration']}
\    ${readvalue2}=    Set Variable     ${edge[${item}]['node']['tags']}
\    ${tag_Count}=    get length     ${edge[${item}]['node']['tags']}
\    ${tag_variable}=   set variable   ${edge[${item}]['node']['tags']}
\    forkeyword       ${tag_Count}   ${tag_variable}    ${readName}

 ${req_json}    Json.Dumps    ${dict}
 Create File  results.json  ${req_json}


forkeyword
[Arguments]       ${tag_Count}      ${tag_variable}     ${readName}
@{z}=   create list
: FOR    ${item}    IN RANGE   0    ${tag_Count}
         \   ${resourceName}=    run keyword if     ${tag_Count} > 0   set variable    ${readName['name']}
         \   log to console  ${resourceName}-forloop
         \   ${readkey}=     set variable   ${tag_variable[${item}]['name']}
         \   ${readvalue}=     set variable   ${tag_variable[${item}]['value']}
         \   set to dictionary     ${dict}     resourceName   ${resourceName}
         \   set to dictionary  ${dict}    ${readkey}     ${readvalue}
set suite variable ${dict}

I expected all values from all tags but only the last tags key and value is getting printed. Can anyone please guide me on the code.

Thanks in advance for any help!!

Mia
  • 448
  • 8
  • 22
  • @robotframework, Can anyone guide on this code – Mia Nov 13 '19 at 11:52
  • @robotframework users, I am stuck in this code can you help me to solve the issue. I just need to get all tags keys and values and store it in json file. – Mia Nov 14 '19 at 07:41

1 Answers1

1

Why do you have duplicate keys in first place?

Duplicate keys in JSON aren't covered by the spec and can lead to undefined behavior. If you read the JSON into a Python dict, the information will be lost, since Python dict keys must be unique. Your best bet is to change the JSON; duplicate keys are a bad idea

Also if you change the json to have no duplicates it gives out the correct result as python keys must be unique.

Since all the other keys are duplicate your simplified version would be:

"data": {
  "resources": {
    "edges": [
      {
        "node": {
          "tags": [
            {
              "name": "app",
              "value": "e2e"
            },
            {
              "name": "Cost",
              "value": "qwert"
            },
            {
              "name": "test",
              "value": "qwerty"
            }
          ]
        }
      }
    ]
  }
}

So if your robot framework gives out the result as

            {
              "name": "app",
              "value": "e2e"
            },
            {
              "name": "Cost",
              "value": "qwert"
            },
            {
              "name": "test",
              "value": "qwerty"
            }

It is correct.

AutoTester213
  • 2,714
  • 2
  • 24
  • 48
  • Sorry If i was not clear!. I need to fetch all the tags response. if you see in the response there are two tags response. So i need to fetch all and append them in a file for which i need help in code. – Mia Nov 14 '19 at 05:27
  • understood! If there dict does not support Duplicate keys. Is there any other way to get them and store it. – Mia Nov 14 '19 at 05:40
  • The duplicates should be avoided in a json, but this might helped https://stackoverflow.com/questions/54357405/combine-duplicate-keys-in-json – AutoTester213 Nov 18 '19 at 08:43