Skip to content

Commit ab3b85a

Browse files
authored
Merge pull request #27 from arnavk09/arnavk09
json to yaml added, branch conflicts sorted
2 parents a315508 + 55fb323 commit ab3b85a

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,6 @@ Create a star pattern pyramid
6262
calculate compound interest
6363

6464
## Script 14 - Mouse mover
65-
Moves your mouse every 15 seconds
65+
Moves your mouse every 15 seconds
66+
## Script 15 - JSON to YAML converter
67+
Converts JSON file to YAML files. A sample JSON is included for testing.

json_2_yaml/json_to_yaml.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# script to convert json to yaml
2+
import json
3+
import os
4+
import sys
5+
import yaml
6+
if len(sys.argv) > 1:
7+
if os.path.exists(sys.argv[1]):
8+
sourcefile = open(sys.argv[1], "r")
9+
contentsource = json.load(sourcefile)
10+
sourcefile.close()
11+
else:
12+
print("ERROR: " + sys.argv[1] + " not found")
13+
exit(1)
14+
else:
15+
print("Usage: json2yaml.py <sourcefile.json> [target_file.yaml]")
16+
outs = yaml.dump(contentsource)
17+
if len(sys.argv) < 3:
18+
print(outs)
19+
elif os.path.exists(sys.argv[2]):
20+
print("ERROR: " + sys.argv[2] + " already exists")
21+
exit(1)
22+
else:
23+
target_file = open(sys.argv[2], "w")
24+
target_file.write(outs)
25+
target_file.close()
26+
##end

json_2_yaml/test.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"quiz": {
3+
"sport": {
4+
"q1": {
5+
"question": "Which one is correct team name in NBA?",
6+
"options": [
7+
"New York Bulls",
8+
"Los Angeles Kings",
9+
"Golden State Warriros",
10+
"Huston Rocket"
11+
],
12+
"answer": "Huston Rocket"
13+
}
14+
},
15+
"maths": {
16+
"q1": {
17+
"question": "5 + 7 = ?",
18+
"options": ["10", "11", "12", "13"],
19+
"answer": "12"
20+
},
21+
"q2": {
22+
"question": "12 - 8 = ?",
23+
"options": ["1", "2", "3", "4"],
24+
"answer": "4"
25+
}
26+
}
27+
}
28+
}

json_2_yaml/test.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
quiz:
2+
maths:
3+
q1:
4+
answer: '12'
5+
options:
6+
- '10'
7+
- '11'
8+
- '12'
9+
- '13'
10+
question: 5 + 7 = ?
11+
q2:
12+
answer: '4'
13+
options:
14+
- '1'
15+
- '2'
16+
- '3'
17+
- '4'
18+
question: 12 - 8 = ?
19+
sport:
20+
q1:
21+
answer: Huston Rocket
22+
options:
23+
- New York Bulls
24+
- Los Angeles Kings
25+
- Golden State Warriros
26+
- Huston Rocket
27+
question: Which one is correct team name in NBA?

0 commit comments

Comments
 (0)