Reputation: 45
I have multiple yaml files in a directory and want to create a single json from them with selected nodes from each file i.e. 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)
Upvotes: 0
Views: 576
Reputation: 469
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)
Upvotes: 1