-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson_cleaner.py
executable file
·52 lines (47 loc) · 1.76 KB
/
json_cleaner.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
"""
A simple library to format JSON strings in the format that I wanted them.
Removes linebreaks that do not correspond to the start of a JSON object
"""
import re
import json
def clean_json_dump(object, fd, *args, **kwargs):
"""
Dump JSON representation of an object to a file
>>> clean_json_dump({'hello': 'world'},
open('file_out.json', 'w'),
indent=2, sort_keys=True)
:param object: The object to dump to the json file
:param fd: File descriptor to which the json should be dumped
:param *args: args to supply to json.dumps
:param **kwargs: keyword arguments to supply to json.dumps
"""
fd.write(clean_json_dumps(object, *args, **kwargs))
def clean_json_dumps(object, *args, **kwargs):
"""
Dump JSON representation of an object to a string
>>> clean_json_dump({'hello': 'world'}, indent=2)
:param object: The object to dump to json string
:param *args: args to supply to json.dumps
:param **kwargs: keyword arguments to supply to json.dumps
:return: The string representation
"""
return clean_json(json.dumps(object, *args, **kwargs))
def clean_json(json_string):
"""
Cleans a json string, removing linebreaks that do not correspond
to the start of a JSON object (as oppsed to a list member)
"""
new_lines = []
in_list = False
for line in json_string.split('\n'):
if ']' in line:
in_list = False
new_lines[-1] += line.strip()
elif '[' in line:
in_list = True
new_lines.append(line + ' ')
elif in_list and ':' not in line and line.strip()[0] != '{':
new_lines[-1] += line.strip() + ' '
else:
new_lines.append(line)
return '\n'.join(new_lines)