-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjson2yaml.py
40 lines (32 loc) · 1006 Bytes
/
json2yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from json import load
from yaml import dump
from sys import argv
'''
This function gets the input and output files from the command line or
straight ask for them with input
'''
def getInputAndOutputFiles():
if len(argv) ==1:
jsonPath = input("please enter the path to your JSON file: ")
yamlPath = input("please enter the path to your YAML file: ")
elif len(argv) == 2:
jsonPath = argv[1]
yamlPath = input("please enter the path to your YAML file: ")
elif len(argv) >= 3:
jsonPath = argv[1]
yamlPath = argv[2]
return jsonPath,yamlPath
'''
This function return the json value from the file path that it's passed
as the parameter
'''
def getJSONValueFromFile(jsonPath):
with open(jsonPath, "r") as jsonFile:
return load(jsonFile)
jsonPath,yamlPath = getInputAndOutputFiles()
print("started to convert your file...")
jsonValue = getJSONValueFromFile(jsonPath)
yamlFile = open(yamlPath, "w")
dump(jsonValue, yamlFile)
yamlFile.close()
print(f"done, your file is now on {yamlPath}")