Skip to content

Commit 0bae697

Browse files
committed
JSON - YAML Converter Added
1 parent c2cb6ce commit 0bae697

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

JSON - YAML Converter/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# JSON - YAML Converter Script
2+
The script can convert the JSON to YAML file, and the YAML to JSON file
3+
4+
What the program does?
5+
- Can accepts single, multiple files
6+
- Convert JSON to YAML
7+
- Convert YAML to JSON
8+
- Single Operation at a time
9+
10+
### Requirements
11+
> Python3
12+
>
13+
> pip install -r requirements.txt
14+
15+
### Usage
16+
```
17+
converter.py [-h] -f [FILE] [-j] [-y]
18+
19+
Convert JSON to YAML and YAML to JSON
20+
21+
optional arguments:
22+
-h, --help show this help message and exit
23+
-f [FILE] Submit a file(s)
24+
-j Convert YAML to JSON
25+
-y Convert JSON to YAML
26+
```
27+
28+
### Contribution
29+
Any kind of contributions are welcome.
30+
31+
1. Fork the Project
32+
2. Commit your Changes
33+
3. Open a Pull Request

JSON - YAML Converter/converter.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import argparse
2+
import json
3+
import os
4+
import yaml
5+
6+
7+
def json2yaml(files):
8+
for file in files:
9+
# Iterate through the files
10+
if os.path.isfile(f"{os.getcwd()}\{file}"):
11+
# Get the base filename
12+
filename_full = os.path.basename(file)
13+
# Get the filename and extension
14+
filename, extension = os.path.splitext(filename_full)
15+
16+
# Opening the file
17+
source_file = open(filename_full, "r")
18+
source_content = json.load(source_file)
19+
source_file.close()
20+
21+
# Processing the conversion
22+
output = yaml.dump(source_content)
23+
24+
# If the target file already exists exit
25+
new_filename = f'{filename}.yaml'
26+
if os.path.exists(new_filename):
27+
print("ERROR: " + new_filename + " already exists")
28+
29+
# Otherwise write to the specified file
30+
else:
31+
target_file = open(new_filename, "w")
32+
target_file.write(output)
33+
target_file.close()
34+
print(f"YAML File Converted: {new_filename}")
35+
36+
37+
def yaml2json(files):
38+
for file in files:
39+
# Iterate through the files
40+
if os.path.isfile(f"{os.getcwd()}\{file}"):
41+
# Get the base filename
42+
filename_full = os.path.basename(file)
43+
# Get the filename and extension
44+
filename, extension = os.path.splitext(filename_full)
45+
46+
# Opening the file
47+
source_file = open(filename_full, "r")
48+
source_content = yaml.safe_load(source_file)
49+
source_file.close()
50+
51+
# Processing the conversion
52+
output = json.dumps(source_content)
53+
54+
# If the target file already exists exit
55+
new_filename = f'{filename}.json'
56+
if os.path.exists(new_filename):
57+
print(f"ERROR: {new_filename} already exists")
58+
59+
# Otherwise write to the specified file
60+
else:
61+
target_file = open(new_filename, "w")
62+
target_file.write(output)
63+
target_file.close()
64+
print(f"JSON File Converted: {new_filename}")
65+
66+
67+
def main():
68+
parser = argparse.ArgumentParser(description='Convert JSON to YAML and YAML to JSON')
69+
70+
# Argument is to accept a file(s)
71+
parser.add_argument('-f', dest='file', nargs='+', type=str, help='Submit a file(s)', required=True)
72+
73+
# Argument is to convert file to json
74+
parser.add_argument('-j', dest='json', action='store_true', help='Convert YAML to JSON', required=True)
75+
76+
# Argument is to convert file to yaml
77+
parser.add_argument('-y', dest='yaml', action='store_true', help='Convert JSON to YAML', required=True)
78+
79+
args = parser.parse_args()
80+
81+
if args.file:
82+
# Accepts files
83+
files = args.file
84+
85+
if args.json:
86+
# Call to function
87+
yaml2json(files)
88+
89+
if args.yaml:
90+
# Call to function
91+
json2yaml(files)
92+
93+
94+
if __name__ == '__main__':
95+
main()

0 commit comments

Comments
 (0)