简体   繁体   English

使用 Python 遍历嵌套的 JSON 数据

[英]Use Python to iterate through nested JSON data

TASK: I am using a API call to get JSON data from our TeamCity CI tool.任务:我正在使用 API 调用从我们的 TeamCity CI 工具获取 JSON 数据。 We need to identify all those builds which are using old version of msbuild.我们需要识别所有使用旧版本 msbuild 的构建。 We can identify from this API call data { "name": "msbuild_version", "value": "15.0" } At the moment i am saving the entire API call data to a file;我们可以从这个 API 调用数据中识别{ "name": "msbuild_version", "value": "15.0" }目前我正在将整个 API 调用数据保存到一个文件中; however i will later integrate the API call to the same script.但是我稍后会将 API 调用集成到同一个脚本中。 Now to the question at hand;现在到手头的问题; How can i filter this above property ie msbuild_version, to say msbuild_version < 15.0 (ie all msbuild less than version 15.0) and display the corresponding 'id' and 'projectName' under 'buildType';我如何过滤上面的属性,即 msbuild_version,说 msbuild_version < 15.0(即所有 msbuild 低于版本 15.0)并在“buildType”下显示相应的“id”和“projectName”; eg例如

"id": "AIntegration_BTool_BToolBuilds_DraftBuild",
"projectName": "A Integration / B Tool / VAR Builds",

here is a part of the JSON data file:-这是 JSON 数据文件的一部分:-

{
    "project": [{
        "id": "_Root",
        "buildTypes": {
            "buildType": []
        }
    }, {
        "id": "AI_BTool_BToolBuilds",
        "buildTypes": {
            "buildType": [{
                "id": "AI_BTool_BToolBuilds_DraftBuild",
                "projectName": "A I / B Tool / VAR Builds",
                "steps": {
                    "step": [ {
                        "id": "RUNNER_213",
                        "name": "Build",
                        "type": "MSBuild",
                        "properties": {
                            "property": [ {
                                "name": "msbuild_version",
                                "value": "16.0"
                            }, {
                                "name": "run-platform",
                                "value": "x64"
                            }, {
                                "name": "targets",
                                "value": "Build"
                            }, {
                                "name": "teamcity.step.mode",
                                "value": "default"
                            }, {
                                "name": "toolsVersion",
                                "value": "15.0"
                            }]
                        }
                    }, {
                        "id": "RUNNER_228",
                        "name": "temp",
                        "type": "VS.Solution",
                        "properties": {
                            "property": [{
                                "name": "build-file-path",
                                "value": "x"
                            }, {
                                "name": "msbuild_version",
                                "value": "16.0"
                            }, {
                                "name": "vs.version",
                                "value": "vs2019"
                            }]
                        }
                    }]
                }
            }, {
                "id": "AI_BTool_BToolBuilds_ContinuousBuildWithNexusI",
                "projectName": "A I / B Tool / VAR Builds",
                "steps": {
                    "step": [ {
                        "id": "RUNNER_22791",
                        "name": "Build",
                        "type": "MSBuild",
                        "properties": {
                            "property": [{
                                "name": "msbuild_version",
                                "value": "16.0"
                            }, {
                                "name": "run-platform",
                                "value": "x86"
                            }, {
                                "name": "teamcity.step.mode",
                                "value": "default"
                            }, {
                                "name": "toolsVersion",
                                "value": "15.0"
                            }]
                        }
                    }]
                }
            }]
        }
    }, {
        "id": "AI_BTool_BToolBuilds_VARApiBuilds",
        "buildTypes": {
            "buildType": [{
                "id": "AI_BTool_BToolBuilds_CiVARNewSolutionContinuousBuild",
                "projectName": "A I / B Tool / VAR Builds / VAR API builds",
                "steps": {
                    "step": [ {
                        "id": "RUNNER_22791",
                        "name": "Build",
                        "type": "MSBuild",
                        "properties": {
                            "property": [{
                                "name": "msbuilds_version",
                                "value": "15.0"
                            }, {
                                "name": "toolsVersion",
                                "value": "15.0"
                            }]
                        }
                    }]
                }
            }, {
                "id": "AI_BTool_BToolBuilds_VARApiBuilds_CiVARIngestionWindowsServiceNonReleaseBranchBuild",
                "projectName": "A I / B Tool / VAR Builds / VAR API builds",
                "steps": {
                    "step": [{
                        "id": "RUNNER_22790",
                        "name": "Nuget Installer",
                        "type": "jb.nuget.installer",
                        "properties": {
                            "property": [{
                                "name": "nuget.path",
                                "value": "%teamcity.tool.NuGet.CommandLine.4.9.2%"
                            }, {
                                "name": "msbuilds_version",
                                "value": "16.0"
                            }, {
                                "name": "nuget.use.restore",
                                "value": "restore"
                            }, {
                                "name": "sln.path",
                                "value": "VAR.sln"
                            }, {
                                "name": "teamcity.step.mode",
                                "value": "default"
                            }]
                        }
                    }]
                }
            }]
        }
    }]
}


My Solution till now And my code snippet till now我的解决方案到现在为止我的代码片段直到现在

import json

with open('UnArchivedBuilds.txt') as api_call:
  read_content = json.load(api_call)

#for project in read_content['project']:
#   print (project.get('buildTypes'))


for project in read_content['project']:
   # print (project['id'])
   print (project['buildTypes']['buildType'])

I am not able to decide on the hierarchy of the JSON to print the relevant data (ie id and projectName) wherever msbuild_version is less than 15.0我无法决定 JSON 的层次结构以在 msbuild_version 小于 15.0 的任何地方打印相关数据(即 id 和 projectName)

I had a look at your JSON data which was broken.我查看了您损坏的 JSON 数据。 In order to work with the snippet you provided I fixed the malformed data and removed unneeded parts to decrease clutter:为了使用您提供的代码段,我修复了格式错误的数据并删除了不需要的部分以减少混乱:

{
    "project": [{
        "buildTypes": {
            "buildType": [{
                "id": "AIntegration_BTool_BToolBuilds_DraftBuild",
                "projectName": "A Integration / B Tool / VAR Builds"
            },
            {
                "id": "AIntegration_BTool_BToolBuilds_ContinuousBuildIntegration",
                "projectName": "A Integration / B Tool / VAR Builds"
            }]
        }
    }]
}

As per my comment above I suggested recursion or using a schema validator to boil the JSON data down.根据我上面的评论,我建议使用递归或使用模式验证器来简化 JSON 数据。 However, as a quick-and-dirty approach and due to the weird structure of your dataset, I decided to do a multi-iteration over some parts of your data.但是,作为一种快速而肮脏的方法,并且由于数据集的奇怪结构,我决定对数据的某些部分进行多次迭代。 Iterating over the same data is quite ugly and considered to be bad practice in most cases.迭代相同的数据非常难看,在大多数情况下被认为是不好的做法。

Assuming the data is stored in a file called input.json , the following snippet should give you the desired output:假设数据存储在一个名为input.json的文件中,下面的代码片段应该为您提供所需的输出:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json

with open('input.json') as f:
    data = json.load(f)

projects = (element for element in data.get('project'))
build_types = (element.get('buildTypes') for element in projects)
build_types = (element.get('buildType') for element in build_types)

for item in build_types:
    for element in item:
        identifier = element.get('id')
        project_name = element.get('projectName')
        print('{} --> {}'.format(identifier, project_name))

Printing:印刷:

AIntegration_BTool_BToolBuilds_DraftBuild --> A Integration / B Tool / VAR Builds
AIntegration_BTool_BToolBuilds_ContinuousBuildIntegration --> A Integration / B Tool / VAR Builds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM