From 55fb323b2573cd5ea880f7259e72158ec58a6538 Mon Sep 17 00:00:00 2001 From: arnavk09 Date: Wed, 5 Oct 2022 11:24:00 +0530 Subject: [PATCH] json to yaml added, branch conflicts sorted --- README.md | 4 +++- json_2_yaml/json_to_yaml.py | 26 ++++++++++++++++++++++++++ json_2_yaml/test.json | 28 ++++++++++++++++++++++++++++ json_2_yaml/test.yaml | 27 +++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 json_2_yaml/json_to_yaml.py create mode 100644 json_2_yaml/test.json create mode 100644 json_2_yaml/test.yaml diff --git a/README.md b/README.md index c773d85a..fb56573d 100644 --- a/README.md +++ b/README.md @@ -62,4 +62,6 @@ Create a star pattern pyramid calculate compound interest ## Script 14 - Mouse mover -Moves your mouse every 15 seconds \ No newline at end of file +Moves your mouse every 15 seconds +## Script 15 - JSON to YAML converter +Converts JSON file to YAML files. A sample JSON is included for testing. \ No newline at end of file diff --git a/json_2_yaml/json_to_yaml.py b/json_2_yaml/json_to_yaml.py new file mode 100644 index 00000000..e8eff39b --- /dev/null +++ b/json_2_yaml/json_to_yaml.py @@ -0,0 +1,26 @@ +# script to convert json to yaml +import json +import os +import sys +import yaml +if len(sys.argv) > 1: + if os.path.exists(sys.argv[1]): + sourcefile = open(sys.argv[1], "r") + contentsource = json.load(sourcefile) + sourcefile.close() + else: + print("ERROR: " + sys.argv[1] + " not found") + exit(1) +else: + print("Usage: json2yaml.py [target_file.yaml]") +outs = yaml.dump(contentsource) +if len(sys.argv) < 3: + print(outs) +elif os.path.exists(sys.argv[2]): + print("ERROR: " + sys.argv[2] + " already exists") + exit(1) +else: + target_file = open(sys.argv[2], "w") + target_file.write(outs) + target_file.close() +##end \ No newline at end of file diff --git a/json_2_yaml/test.json b/json_2_yaml/test.json new file mode 100644 index 00000000..d31d70a6 --- /dev/null +++ b/json_2_yaml/test.json @@ -0,0 +1,28 @@ +{ + "quiz": { + "sport": { + "q1": { + "question": "Which one is correct team name in NBA?", + "options": [ + "New York Bulls", + "Los Angeles Kings", + "Golden State Warriros", + "Huston Rocket" + ], + "answer": "Huston Rocket" + } + }, + "maths": { + "q1": { + "question": "5 + 7 = ?", + "options": ["10", "11", "12", "13"], + "answer": "12" + }, + "q2": { + "question": "12 - 8 = ?", + "options": ["1", "2", "3", "4"], + "answer": "4" + } + } + } +} diff --git a/json_2_yaml/test.yaml b/json_2_yaml/test.yaml new file mode 100644 index 00000000..d26a5257 --- /dev/null +++ b/json_2_yaml/test.yaml @@ -0,0 +1,27 @@ +quiz: + maths: + q1: + answer: '12' + options: + - '10' + - '11' + - '12' + - '13' + question: 5 + 7 = ? + q2: + answer: '4' + options: + - '1' + - '2' + - '3' + - '4' + question: 12 - 8 = ? + sport: + q1: + answer: Huston Rocket + options: + - New York Bulls + - Los Angeles Kings + - Golden State Warriros + - Huston Rocket + question: Which one is correct team name in NBA?