Skip to content

Commit fccb841

Browse files
authored
Merge pull request #57 from Siddhant-K-code/master
Script to Convert JSON to YAML and Vice Versa
2 parents 57bfd73 + 891286c commit fccb841

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

JSON-YAML/JSON-to-YAML.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# If you work with config files or need to expose YAML via an API, you’ll probably find yourself needing to convert a file from JSON to YAML. The output can either be sent to stdout or to a specified file.
2+
3+
import json
4+
import os
5+
import sys
6+
import yaml
7+
8+
# Checking there is a file name passed
9+
if len(sys.argv) > 1:
10+
# Opening the file
11+
if os.path.exists(sys.argv[1]):
12+
source_file = open(sys.argv[1], "r")
13+
source_content = json.load(source_file)
14+
source_file.close()
15+
# Failikng if the file isn't found
16+
else:
17+
print("ERROR: " + sys.argv[1] + " not found")
18+
exit(1)
19+
# No file, no usage
20+
else:
21+
print("Usage: json2yaml.py <source_file.json> [target_file.yaml]")
22+
23+
# Processing the conversion
24+
output = yaml.dump(source_content)
25+
26+
# If no target file send to stdout
27+
if len(sys.argv) < 3:
28+
print(output)
29+
# If the target file already exists exit
30+
elif os.path.exists(sys.argv[2]):
31+
print("ERROR: " + sys.argv[2] + " already exists")
32+
exit(1)
33+
# Otherwise write to the specified file
34+
else:
35+
target_file = open(sys.argv[2], "w")
36+
target_file.write(output)
37+
target_file.close()
38+
39+
# To Run It :
40+
# json2yaml.py input_file.json output_file.yaml

JSON-YAML/YAML-to-JSON.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This script converts a file from YAML to JSON. The output can either be sent to stdout or to a specified file.
2+
3+
import json
4+
import os
5+
import sys
6+
import yaml
7+
8+
# Checking there is a file name passed
9+
if len(sys.argv) > 1:
10+
# Opening the file
11+
if os.path.exists(sys.argv[1]):
12+
source_file = open(sys.argv[1], "r")
13+
source_content = yaml.safe_load(source_file)
14+
source_file.close()
15+
# Failikng if the file isn't found
16+
else:
17+
print("ERROR: " + sys.argv[1] + " not found")
18+
exit(1)
19+
# No file, no usage
20+
else:
21+
print("Usage: yaml2json.py <source_file.yaml> [target_file.json]")
22+
23+
# Processing the conversion
24+
output = json.dumps(source_content)
25+
26+
# If no target file send to stdout
27+
if len(sys.argv) < 3:
28+
print(output)
29+
# If the target file already exists exit
30+
elif os.path.exists(sys.argv[2]):
31+
print("ERROR: " + sys.argv[2] + " already exists")
32+
exit(1)
33+
# Otherwise write to the specified file
34+
else:
35+
target_file = open(sys.argv[2], "w")
36+
target_file.write(output)
37+
target_file.close()
38+
39+
40+
# To run it:
41+
42+
# yaml2json.py input_file.yaml output_file.json

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ This is a collection of short Python scripts to solve and automate tasks and sim
5050
30 | [**EC2-Instance-Launcher**](https://github.com/DiptoChakrabarty/Useful-Python-scripts-collection/blob/master/EC2-launcher/ec2.py) | Python script to automatically launch ec2 instances in AWS | boto3 , botocore , python-dotenv , environmental variables
5151
31 | [**DirectorySummarizer**](https://github.com/j0fiN/Useful-Python-scripts-collection/blob/master/DirectorySummarizer/directory_summarizer.py) | A Python script which summarizes the number of different files in a directory. It also gives out the percentage of a particular file extension you want to know. | None |
5252
31 | [**Folder-Automater**](https://github.com/decipher07/Useful-Python-scripts-collection/blob/master/Folder-Automater/automateall.py) | A Python script which compiles all the different formats of files present in the respective folder to a new folder containing only the Specified files . | None |
53-
32 | [**CSV_to_JSON**](https://github.com/TechBoyy6/Python-scripts-collection/tree/master/CSV_to_JSON) | A script that converts the csv file into json file. | None |
53+
32 | [**JSON-YAML**](https://github.com/decipher07/Useful-Python-scripts-collection/blob/master/JSON-YAML) | A Python script which converts JSON To YAML and Vice-Versa | JSON, YAML |
54+
33 | [**CSV_to_JSON**](https://github.com/TechBoyy6/Python-scripts-collection/tree/master/CSV_to_JSON) | A script that converts the csv file into json file. | None |

0 commit comments

Comments
 (0)