-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_json_parser.py
47 lines (32 loc) · 1.29 KB
/
xml_json_parser.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
# Module which takes json or xml string , parse it to python Dict object.
# returns empty dict, if its invalid data type or invalid xml or json format.
# Make sure you have all the modules installed.
# Author :: Amit Yadav
import json
import xmltodict
def xml_json_parse(string):
simple_dict = dict()
if type(string) != str:
print('ERROR :: "Not A valid data type, please pass a string." ')
print(f'Object type :: "{type(string)}" ')
return simple_dict
try:
simple_dict = json.loads(string)
# Need to change to dict if its just one value. ( maybe string or integer )
if type(simple_dict) != dict:
value = simple_dict
simple_dict = dict()
simple_dict[value] = value
except:
# comes here if the string is not a valid json string.
try:
simple_dict = xmltodict.parse(string)
# Need to change to dict if its just one value. ( maybe string or integer )
if type(simple_dict) != dict:
value = simple_dict
simple_dict = dict()
simple_dict[value] = value
except:
# if neither json or xml string.
print('ERROR :: "Not a valid xml or json string." ')
return simple_dict