简体   繁体   中英

Python - Create a single Json from multiple Yaml file of a directory

I have multiple yaml files in a directory and want to create a single json from them with selected nodes from each file ie we need only 2 to 4 json nodes from a file and not all.

I am able to read the Yaml and Convert to json but not getting how to select the required nodes from each json/yaml file and create a final single json.

Can some one suggest me how to select the nodes from all Yaml file and append into a new Json file?

Using below code, I'm able to convert all YAML file to Separate Json file but not getting how to select nodes from each and create a new Json file.

import yaml
import glob

files = glob.glob("C:\\Users\\test\\*.yaml")  # list of all .yaml files in a directory


def read_yaml_file(filename):
    with open(filename, 'r') as stream:
        try:
            print(yaml.safe_load(stream))
        except yaml.YAMLError as exc:
            print(exc)


for file in files:
    data = read_yaml_file(file)

It's hard to give you advice without knowing data structure and what are you trying to acheive but here is example with creating json list of all node_name keys from all yaml files:

import yaml
import json
import glob

files = glob.glob("C:\\Users\\test\\*.yaml")  # list of all .yaml files in a directory


def read_yaml_file(filename):
    with open(filename, 'r') as stream:
        try:
            return yaml.safe_load(stream)
        except yaml.YAMLError as exc:
            print(exc)
            return None

yaml_files_data = [read_yaml_file(f) for f in files]
json_data = []

for yaml_data in yaml_files_data:
    if yaml_data is None: # error occured in `read_yaml_file`
        continue
    json_data.append(yaml_data["node_name"])

with open("output.json", "w") as file:
    json.dump(json_data, file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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