简体   繁体   English

Python - 如何从 Json 中提取特定节点并创建新的 Json

[英]Python - How to extract specific Nodes from Json and Create a new Json

I have a requirement to read multiple yaml files and extract some nodes from all and finally create a single output json file.我需要读取多个 yaml 文件并从所有文件中提取一些节点,最后创建一个 output json 文件。 with below code, I am able to convert required files as json and able to print in console but not getting that how can I extract some nodes from each json/yaml and append into new final (single) output json. with below code, I am able to convert required files as json and able to print in console but not getting that how can I extract some nodes from each json/yaml and append into new final (single) output json. I have to extract json nodes with Name only and not by index number as it may get changed.我必须仅使用名称而不是索引号提取 json 节点,因为它可能会更改。 Please help.请帮忙。 lets assume that I want "resources" node from one json and accordingly some nodes from another json/yaml and finally build a output json with all selected nodes from each file.假设我想要来自一个 json 的“资源”节点,因此需要来自另一个 json/yaml 的一些节点,最后构建一个 output json 和每个文件中的所有选定节点。

path = "C:\\Users\\path\\temp"


import os
import json

filelist = []


for root, dirs, filename in os.walk(path):
    for file in filename:
#append the file name to the list
        filelist.append(os.path.join(root,file))
#filter only yaml file
yaml_files_list = [ fi for fi in filelist if fi.endswith("yaml") ]

#read yaml file
for files in yaml_files_list :
            #get file name only :
            filename = files.split(os.sep)[-1]
            if(filename == "doms.yaml" or filename == "cells.yaml" or filename == "nc.yaml" or filename == "dc.yaml"):
             with open(files) as file:
                yaml_data = yaml.safe_load(file)
            #if str(yaml_data).startswith("[") then remove it to make simple json string:
                data = str(yaml_data)[1:-1]
                json_data = json.dumps(data)
                print(json_data)
                json_data

            elif (filename == "ances.yaml" or filename == "ages.yaml" or filename == "set.yaml"):
             with open(files) as file:
                 yaml_data = yaml.safe_load(file)
                 json_data = json.dumps(yaml_data)
                 print(json_data)
                 json_data

            else:
                print("nothing")

############################ ############################

Printed Data in Console from all 7 Yaml files:控制台中来自所有 7 个 Yaml 文件的打印数据:

{"adminPassword": "<BASE64 encoded password>", "ipIndex": 9, "name": "vc", "rootPassword": "<BASE64 encoded password>", "type": "VC", "vcfPassword": "<BASE64 encoded password>", "auditPassword": "<BASE64 encoded password>", "size": "large", "tier0Mode": "ACTIVE_STANDBY", "ipIndexPool": [{"end": 80, "start": 60}, {"end": 101, "start": 91}], "nodeCount": 4, "shares": [{"name": "second-share", "quotaInMb": "10240"}]}
"{'enabled': True, 'name': 'cell01', 'networks': [{'mtu': 1500, 'name': 'management', 'segmentType': 'vlan', 'switch': 'dvs-cell', 'type': 'management', 'vlan': 0}], 'parent': 'regional01', 'switches': [{'name': 'dvs-cell', 'uplinks': [{'pnic': 'vmnic4'}, {'pnic': 'vmnic6'}]}], 'type': 'CELL_SITE_GROUP'}"
{"cloudbuilder": "http://www.nfv.com/nfv_share/VMware-Cloud-Builder-4.2.0.0-17559673_OVF10.ova", "haproxy": ["http://www.nfv.com/nfv_share/photon-3-kube-v1.20.4.ova"], "kube": ["http://www.nfv.com/nfv_share/photon-3-kube-v1.20.4.ova"], "tca": "http://www.nfv.com/nfv_share/VMware-Telco-Cloud-Automation-1.9.1-18002366.ova", "vrli": "http://www.nfv.com/nfv_share/VMware-vRealize-Log-Insight-8.3.0.0-17494646_OVF10.ova", "vro": "http://www.nfv.com/nfv_share/O11N_VA-8.3.0.15012-17535332_OVF10.ova", "vsphere_plugin": "http://www.nfv.com/nfv_share/vco-plugin-7.4.0.13701555.zip"}
"{'enabled': True, 'licenses': {'esxi': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'nsx': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'tca': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'tca_cp': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vc': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vrli': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vsan': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx']}, 'location': {'address': '', 'city': 'Palo Alto', 'country': 'United States of America', 'latitude': 43.6722, 'longitude': -92.9761}, 'minimumHosts': 4, 'name': 'ndc', 'networks': [{'_comments': ['This network requires DHCP configured on the network'], 'gateway': '192.168.20.254', 'mtu': 9000, 'name': 'management', 'network': '192.168.20.0', 'prefixLength': 24, 'switch': 'dvs-central', 'type': 'management', 'vlan': 20}, {'gateway': '192.168.2.254', 'ipPool': [{'end': '192.168.2.13', 'start': '192.168.2.10'}], 'mtu': 9000, 'name': 'vMotion', 'network': '192.168.2.0', 'prefixLength': 24, 'switch': 'dvs-central', 'type': 'vMotion', 'vlan': 200}, {'gateway': '192.168.3.254', 'ipPool': [{'end': '192.168.3.13', 'start': '192.168.3.10'}], 'mtu': 9000, 'name': 'vSAN', 'network': '192.168.3.0', 'prefixLength': 24, 'switch': 'dvs-central', 'type': 'vSAN', 'vlan': 300}, {'_comments': ['This network requires DHCP configured on the network'], 'gateway': '192.168.7.254', 'mtu': 9000, 'name': 'nsxHostOverlay', 'network': '192.168.7.0', 'prefixLength': 24, 'switch': 'dvs-central', 'type': 'nsxHostOverlay', 'vlan': 700}, {'gateway': '192.168.8.254', 'mtu': 9000, 'name': 'nsxEdgeOverlay', 'network': '192.168.8.0', 'prefixLength': 24, 'switch': 'dvs-central', 'type': 'nsxEdgeOverlay', 'vlan': 800}, {'gateway': '192.168.40.254', 'ipAddresses': ['192.168.40.10', '192.168.40.20'], 'mtu': 9000, 'name': 'uplink1', 'prefixLength': 24, 'switch': 'dvs-central', 'type': 'uplink', 'vlan': 40}, {'gateway': '192.168.30.254', 'ipAddresses': ['192.168.30.10', '192.168.30.20'], 'mtu': 9000, 'name': 'uplink2', 'prefixLength': 24, 'switch': 'dvs-central', 'type': 'uplink', 'vlan': 30}], 'services': [{'enabled': False, 'name': 'networking', 'nsxConfig': {'shareOverlayTransportZoneWithParent': False}, 'type': 'nsx'}, {'enabled': False, 'name': 'storage', 'type': 'vsan'}], 'switches': [{'name': 'dvs-central', 'uplinks': [{'pnic': 'vmnic4'}, {'pnic': 'vmnic6'}]}], 'type': 'CENTRAL_SITE'}"
"{'applianceOverrides': [{'adminPassword': '<BASE64 encoded password>', 'ipIndex': 8, 'name': 'vc', 'rootPassword': '<BASE64 encoded password>', 'type': 'VC'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 5, 'name': 'sddc-manager', 'rootPassword': '<BASE64 encoded password>', 'type': 'SDDC_MANAGER', 'vcfPassword': '<BASE64 encoded password>'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 1, 'name': 'cloud-builder', 'rootPassword': '<BASE64 encoded password>', 'type': 'CLOUD_BUILDER', 'vcfPassword': '<BASE64 encoded password>'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 19, 'name': 'vro', 'rootPassword': '<BASE64 encoded password>', 'type': 'VRO'}, {'adminPassword': '<BASE64 encoded password>', 'auditPassword': '<BASE64 encoded password>', 'ipIndex': 30, 'name': 'nsx', 'rootPassword': '<BASE64 encoded password>', 'type': 'NSX_MANAGER'}, {'ipIndex': 31, 'name': 'nsx001', 'parent': 'nsx', 'type': 'NSX_MANAGER_NODE'}, {'ipIndex': 32, 'name': 'nsx002', 'parent': 'nsx', 'type': 'NSX_MANAGER_NODE'}, {'ipIndex': 33, 'name': 'nsx003', 'parent': 'nsx', 'type': 'NSX_MANAGER_NODE'}, {'adminPassword': '<BASE64 encoded password>', 'auditPassword': '<BASE64 encoded password>', 'name': 'edgeCluster001', 'rootPassword': '<BASE64 encoded password>', 'size': 'large', 'tier0Mode': 'ACTIVE_STANDBY', 'type': 'NSX_EDGE_CLUSTER'}, {'ipIndex': 112, 'name': 'edge001', 'parent': 'edgeCluster001', 'type': 'NSX_EDGE'}, {'ipIndex': 113, 'name': 'edge002', 'parent': 'edgeCluster001', 'type': 'NSX_EDGE'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 7, 'name': 'tca-cp', 'rootPassword': '<BASE64 encoded password>', 'type': 'TCA_CP'}, {'ipIndex': 111, 'name': 'vrli', 'type': 'VRLI'}, {'_comment': [], 'ipIndexPool': [{'end': 90, 'start': 81}], 'name': 'vsannfs', 'nodeCount': 4, 'shares': [{'name': 'second-share', 'quotaInMb': '10240'}], 'type': 'VSAN_NFS'}, {'adminPassword': '<BASE64 encoded password>', 'auditPassword': '<BASE64 encoded password>', 'name': 'edgeCluster001', 'nameOverride': 'edgeCluster-cicd', 'rootPassword': '<BASE64 encoded password>', 'size': 'large', 'tier0Mode': 'ACTIVE_STANDBY', 'type': 'NSX_EDGE_CLUSTER'}], 'enabled': True, 'licenses': {'esxi': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'nsx': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'tca_cp': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vc': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vrli': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vsan': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx']}, 'location': {'address': '', 'city': 'Austin', 'country': 'United States of America', 'latitude': 43.6722, 'longitude': -92.9761}, 'minimumHosts': 4, 'name': 'rdc', 'networks': [{'_comments': ['This network requires DHCP configured on the network'], 'gateway': '192.168.20.254', 'mtu': 9000, 'name': 'management', 'network': '192.168.20.0', 'prefixLength': 24, 'switch': 'dvs-regional', 'type': 'management', 'vlan': 20}, {'gateway': '192.168.2.254', 'ipPool': [{'end': '192.168.2.17', 'start': '192.168.2.14'}], 'mtu': 9000, 'name': 'vMotion', 'network': '192.168.2.0', 'prefixLength': 24, 'switch': 'dvs-regional', 'type': 'vMotion', 'vlan': 200}, {'gateway': '192.168.3.254', 'ipPool': [{'end': '192.168.3.17', 'start': '192.168.3.14'}], 'mtu': 9000, 'name': 'vSAN', 'network': '192.168.3.0', 'prefixLength': 24, 'switch': 'dvs-regional', 'type': 'vSAN', 'vlan': 300}, {'_comments': ['This network requires DHCP configured on the network'], 'gateway': '192.168.7.254', 'mtu': 9000, 'name': 'nsxHostOverlay', 'network': '192.168.7.0', 'prefixLength': 24, 'switch': 'dvs-regional', 'type': 'nsxHostOverlay', 'vlan': 700}, {'gateway': '192.168.8.254', 'mtu': 9000, 'name': 'nsxEdgeOverlay', 'network': '192.168.8.0', 'prefixLength': 24, 'switch': 'dvs-regional', 'type': 'nsxEdgeOverlay', 'vlan': 800}, {'gateway': '192.168.40.254', 'ipAddresses': ['192.168.40.11', '192.168.40.21'], 'mtu': 9000, 'name': 'uplink1', 'prefixLength': 24, 'switch': 'dvs-regional', 'type': 'uplink', 'vlan': 40}, {'gateway': '192.168.30.254', 'ipAddresses': ['192.168.30.11', '192.168.30.21'], 'mtu': 9000, 'name': 'uplink2', 'prefixLength': 24, 'switch': 'dvs-regional', 'type': 'uplink', 'vlan': 30}], 'services': [{'enabled': False, 'name': 'networking', 'nsxConfig': {'shareOverlayTransportZoneWithParent': False}, 'type': 'nsx'}, {'enabled': False, 'name': 'storage', 'type': 'vsan'}], 'switches': [{'name': 'dvs-regional', 'uplinks': [{'pnic': 'vmnic4'}, {'pnic': 'vmnic6'}]}], 'type': 'REGIONAL_SITE'}"
{"dnsServers": ["192.168.20.250"], "dnsSuffix": "dellnfv.com", "ntpServers": ["192.168.20.249"], "saas": "connect.tec.vmware.com"}

Final Desired Output as Sample:最终所需的 Output 作为样品:

{"adminPassword": "<BASE64 encoded password>", "ipIndex": 9, "name": "vc", "rootPassword": "<BASE64 encoded password>", "type": "VC", "vcfPassword": "<BASE64 encoded password>", "auditPassword": "<BASE64 encoded password>", "size": "large",'networks': [{'mtu': 1500, 'name': 'management', 'segmentType': 'vlan', 'switch': 'dvs-cell', 'type': 'management', 'vlan': 0}]'type': 'CELL_SITE_GROUP', "cloudbuilder": "http://www.nfv.com/nfv_share/VMware-Cloud-Builder-4.2.0.0-17559673_OVF10.ova", "haproxy": ["http://www.nfv.com/nfv_share/photon-3-kube-v1.20.4.ova"], "kube": ["http://www.nfv.com/nfv_share/photon-3-kube-v1.20.4.ova"], "tca": "http://www.nfv.com/nfv_share/VMware-Telco-Cloud-Automation-1.9.1-18002366.ova", "vrli": "http://www.nfv.com/nfv_share/VMware-vRealize-Log-Insight-8.3.0.0-17494646_OVF10.ova", "vro": "http://www.nfv.com/nfv_share/O11N_VA-8.3.0.15012-17535332_OVF10.ova",'enabled': True, 'licenses': {'esxi': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'nsx': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'tca': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'tca_cp': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vc': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'], 'vrli': ['xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'],'applianceOverrides': [{'adminPassword': '<BASE64 encoded password>', 'ipIndex': 8, 'name': 'vc', 'rootPassword': '<BASE64 encoded password>', 'type': 'VC'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 5, 'name': 'sddc-manager', 'rootPassword': '<BASE64 encoded password>', 'type': 'SDDC_MANAGER', 'vcfPassword': '<BASE64 encoded password>'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 1, 'name': 'cloud-builder', 'rootPassword': '<BASE64 encoded password>', 'type': 'CLOUD_BUILDER', 'vcfPassword': '<BASE64 encoded password>'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 19, 'name': 'vro', 'rootPassword': '<BASE64 encoded password>', 'type': 'VRO'}, {'adminPassword': '<BASE64 encoded password>', 'auditPassword': '<BASE64 encoded password>', 'ipIndex': 30, 'name': 'nsx', 'rootPassword': '<BASE64 encoded password>', 'type': 'NSX_MANAGER'}, {'ipIndex': 31, 'name': 'nsx001', 'parent': 'nsx', 'type': 'NSX_MANAGER_NODE'}, {'ipIndex': 32, 'name': 'nsx002', 'parent': 'nsx', 'type': 'NSX_MANAGER_NODE'}, {'ipIndex': 33, 'name': 'nsx003', 'parent': 'nsx', 'type': 'NSX_MANAGER_NODE'}, {'adminPassword': '<BASE64 encoded password>', 'auditPassword': '<BASE64 encoded password>', 'name': 'edgeCluster001', 'rootPassword': '<BASE64 encoded password>', 'size': 'large', 'tier0Mode': 'ACTIVE_STANDBY', 'type': 'NSX_EDGE_CLUSTER'}, {'ipIndex': 112, 'name': 'edge001', 'parent': 'edgeCluster001', 'type': 'NSX_EDGE'}, {'ipIndex': 113, 'name': 'edge002', 'parent': 'edgeCluster001', 'type': 'NSX_EDGE'}, {'adminPassword': '<BASE64 encoded password>', 'ipIndex': 7, 'name': 'tca-cp', 'rootPassword': '<BASE64 encoded password>', 'type': 'TCA_CP'}, {'ipIndex': 111, 'name': 'vrli', 'type': 'VRLI'}, {'_comment': [], 'ipIndexPool': [{'end': 90, 'start': 81}], 'name': 'vsannfs', 'nodeCount': 4, 'shares': [{'name': 'second-share', 'quotaInMb': '10240'}], 'type': 'VSAN_NFS'}, {'adminPassword': '<BASE64 encoded password>', 'auditPassword': '<BASE64 encoded password>', 'name': 'edgeCluster001', 'nameOverride': 'edgeCluster-cicd', 'rootPassword': '<BASE64 encoded password>', 'size': 'large', 'tier0Mode': 'ACTIVE_STANDBY', 'type': 'NSX_EDGE_CLUSTER'}]}}

After parsing the yaml, you have a structure consisting of dictionaries and lists in yaml_data .解析完 yaml 后,您在yaml_data中拥有一个由字典和列表组成的结构。 You then convert it to a string with this line and/or the call to json.dumps :然后,您可以使用此行和/或调用json.dumps将其转换为字符串:

data = str(yaml_data)[1:-1]

Don't do that.不要那样做。 Work with the dictionaries to extract the fields you need, and organize them into a new dictionary (or a list of dictionaries, collecting data from all files).使用字典提取您需要的字段,并将它们组织成一个新字典(或字典列表,从所有文件中收集数据)。 Then write out the results to a file with json.dump()然后使用json.dump()将结果写入文件

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

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