-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathjson2Schema.py
56 lines (51 loc) · 1.94 KB
/
json2Schema.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
while(True):
try:
fileName = input("Enter file name : ")
with open(fileName) as f:
data = json.load(f)
break
except json.decoder.JSONDecodeError as e:
print("Invalid JSON format.",e)
except Exception:
print("Invalid file Name.File not present in current directory.")
continue
def getJsonData(key,value):
if(isinstance(value,(str)) or value is None):
tempDict = {}
data_dict = {"description":"","type":"string","example":value}
tempDict[key] = data_dict
return tempDict
if(isinstance(value,(bool))):
tempDict = {}
data_dict = {"description":"","type":"boolean","example":value}
tempDict[key] = data_dict
return tempDict
if(isinstance(value,(int))):
tempDict = {}
data_dict = {"description":"","type":"integer","example":value}
tempDict[key] = data_dict
return tempDict
if(isinstance(value,dict)):
tempDict = {}
data_dict = {"type":"object","properties":{}}
for key2,value2 in value.items():
data_dict['properties'].update(getJsonData(key2,value2))
tempDict[key] = data_dict
return tempDict
if(isinstance(value,list)):
tempDict = {}
data_dict = {"type":"array","items":{"oneOf":[]}}
for val in value:
data_dict2 = {"type":"object","properties":{}}
data_dict["items"]["oneOf"].append(data_dict2)
for key2,value2 in val.items():
data_dict2["properties"].update(getJsonData(key2,value2))
tempDict[key] = data_dict
return tempDict
result = {"type":"object","properties":{}}
for key,value in data.items():
result["properties"].update(getJsonData(key,value))
with open(fileName.split('.')[0]+'_schema.json', 'w') as fp:
json.dump(result, fp)
print(fileName.split('.')[0]+'_schema.json'+' file generated successfully')