有没有一种方法可以获取多个JSON键和值并将其存储在RobotFramework中的JSON文件中?

jhkqcmku  于 2023-04-08  发布在  其他
关注(0)|答案(1)|浏览(149)

我需要从JSON响应中读取键和值,并使用机器人框架代码将其存储为JSON格式。
我试过机器人框架代码

JSON RESPONSE:

       {
"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"
      }
  }
 }
 ]
 }
 }
 }

我的机器人代码:

${tag_Count}= 3 ##Since there are 3 tags variable in response
  : FOR    ${item}    IN RANGE   0    ${tag_Count}
         \   ${resourceName}=    run keyword if     ${tag_Count} > 0   set variable    ${readName['name']}
         \   log to console  ${resourceName}-forloop
         \   ${resourceName_length}=  get length   ${resourceName}
         \   ${readkey}=     set variable   ${tag_variable[${item}]['name']}
         \   ${readvalue}=     set variable   ${tag_variable[${item}]['value']}

     set to dictionary  ${dict}    ${readkey}     ${readvalue}
     set suite variable   ${dict}
     ${req_json}    Json.Dumps    ${dict}
     Create File  response.json  ${req_json}

我需要在json文件中以这种格式的响应。我怎么做?

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

我想单独获取标签名称和值并存储在JSON文件中。我只能将最后一个标签响应存储在JSON文件中。

flvtvl50

flvtvl501#

这是我的json:

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

这是我的代码(RFW中的JSONPath):

$.node.tags..name

希望这能帮上忙。

相关问题