返回python中对应键值对的对象

问题描述 投票:0回答:1

我收到了来自 API 的 JSON 响应

[ {
        "@type": "activityLogEntry",
        "runContextType": "REST_API_V2",
        "entries": [
            {
                "@type": "activityLogEntry",
                "entries": [],
                "subTaskEntries": [],
                "logEntryItemAttrs": {
                    "CONSUMED_COMPUTE_UNITS": "0.0",
                    "releaseVersion": "4500"
                },
                "sessionVariables": {
                    "$$batch_name": "Taskflow_Fact_Order_Tracking",
                    "$$status": "Already Running",
                    "$$sub_area": "UPS"
                },
                "totalSuccessRows": 0,
                "isStopped": "false",
                "transformationEntries": [
                    {
                        "@type": "transformationLogEntry",
                        "txInstanceName": "src_batch_log"
                    },
                    {
                        "@type": "transformationLogEntry",
                        "affectedRows": 1
                    }
                ]
            }
        ],
        "totalSuccessRows": 0,
        "stopOnError": false,
        "isStopped": "false"
    },
    {
        "@type": "activityLogEntry",
        "runContextType": "REST_API_V2",
        "entries": [
            {
                "@type": "activityLogEntry",
                "entries": [],
                "subTaskEntries": [],
                "logEntryItemAttrs": {
                    "CONSUMED_COMPUTE_UNITS": "0.0",
                    "releaseVersion": "4500"
                },
                "sessionVariables": {
                    "$$batch_name": "Taskflow_Fact_12222",
                    "$$status": "Already Running",
                    "$$sub_area": "UPS"
                },
                "totalSuccessRows": 0,
                "isStopped": "false",
                "transformationEntries": [
                    {
                        "@type": "transformationLogEntry",
                        "txInstanceName": "src_batch_log"
                    },
                    {
                        "@type": "transformationLogEntry",
                        "affectedRows": 1
                    }
                ]
            }
        ],
        "totalSuccessRows": 0,
        "stopOnError": false,
        "isStopped": "false"
    },
    {
        "@type": "activityLogEntry",
        "runContextType": "REST_API_V2",
        "entries": [
            {
                "@type": "activityLogEntry",
                "entries": [],
                "subTaskEntries": [],
                "logEntryItemAttrs": {
                    "CONSUMED_COMPUTE_UNITS": "0.0",
                    "releaseVersion": "4500"
                },
                "sessionVariables": {
                    "$$batch_name": "Taskflow_Fact_12",
                    "$$status": "Already Running",
                    "$$sub_area": "UPS"
                },
                "totalSuccessRows": 0,
                "isStopped": "false",
                "transformationEntries": [
                    {
                        "@type": "transformationLogEntry",
                        "txInstanceName": "src_batch_log"
                    },
                    {
                        "@type": "transformationLogEntry",
                        "affectedRows": 1
                    }
                ]
            }
        ],
        "totalSuccessRows": 0,
        "stopOnError": false,
        "isStopped": "false"
    },
    ]

我希望返回具有键“$$batch_name”和值“Taskflow_Fact_Order_Tracking”的数组元素。

我尝试获取键值..但无法返回整个数组元素。

输出应返回数组的第一个元素。因为它有“$$batch_name”:“Taskflow_Fact_Order_Tracking”,

我特别在Python中寻找解决方案。

谢谢你

python python-3.x
1个回答
0
投票

以下代码使用列表理解来迭代解析的 JSON 数组。

next()
函数检索
$$batch_name
"Taskflow_Fact_Order_Tracking"
的第一个匹配元素。

import json

# Parse the JSON data
activity_logs = json.loads(data)

# Filter the entries
matching_entry = next((entry for entry in activity_logs
                      if entry["entries"][0]["sessionVariables"]["$$batch_name"] == "Taskflow_Fact_Order_Tracking"), None)

# Print the matching entry
print(json.dumps(matching_entry, indent=2))

结果:

{
  "@type": "activityLogEntry",
  "runContextType": "REST_API_V2",
  "entries": [
    {
      "@type": "activityLogEntry",
      "entries": [],
      "subTaskEntries": [],
      "logEntryItemAttrs": {
        "CONSUMED_COMPUTE_UNITS": "0.0",
        "releaseVersion": "4500"
      },
      "sessionVariables": {
        "$$batch_name": "Taskflow_Fact_Order_Tracking",
        "$$status": "Already Running",
        "$$sub_area": "UPS"
      },
      "totalSuccessRows": 0,
      "isStopped": "false",
      "transformationEntries": [
        {
          "@type": "transformationLogEntry",
          "txInstanceName": "src_batch_log"
        },
        {
          "@type": "transformationLogEntry",
          "affectedRows": 1
        }
      ]
    }
  ],
  "totalSuccessRows": 0,
  "stopOnError": false,
  "isStopped": "false"
}
© www.soinside.com 2019 - 2024. All rights reserved.