-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathartifast_json_2_splunk.py
32 lines (27 loc) · 1.32 KB
/
artifast_json_2_splunk.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
#!/usr/bin/env python3
import glob
import re
def process_file(filename):
# Open the file without converting newlines.
with open(filename, 'r', encoding='utf-8', newline='') as f:
content = f.read()
# Apply substitutions
content = re.sub(r'\[', '', content) # remove '['
content = re.sub(r'\]', '', content) # remove ']'
content = re.sub(r' {', '{', content) # replace " {" with "{"
content = re.sub(r'},\{', '}\r\n{', content) # replace "},{" with "}\r\n{"
content = re.sub(r'\{\r\n\s+', '{', content) # remove newline and spaces after '{'
content = re.sub(r',\r\n\s+', ',', content) # remove newline and spaces after comma
content = re.sub(r'\r\n\s+\}', '}', content) # remove newline and spaces before '}'
content = re.sub(r'\r\n\}', '}', content) # remove newline before '}'
# Write back, preserving the newline sequences
with open(filename, 'w', encoding='utf-8', newline='') as f:
f.write(content)
def main():
files = glob.glob('*.json')
total = len(files)
for idx, filename in enumerate(files, start=1):
print(f"Processing file {idx}/{total}: {filename}")
process_file(filename)
if __name__ == '__main__':
main()