Skip to content

Commit 15e81a9

Browse files
committed
Enhance JSON log processing to handle nested and list structures, and improve error handling for concatenated JSON objects
1 parent 7fcdb37 commit 15e81a9

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

zeek-term.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,49 @@ def process_json_log_line(log_type, data):
148148
raw = line.strip()
149149
if not raw:
150150
continue
151+
152+
parsed = False
151153
try:
152154
data = json.loads(raw)
153-
# Only JSON objects should be treated as JSON records.
155+
154156
if isinstance(data, dict):
155157
process_json_log_line(log_type, data)
156-
else:
157-
parts = raw.split('\t')
158-
process_text_log_line(log_type, parts)
159-
except json.JSONDecodeError:
158+
parsed = True
159+
elif isinstance(data, str):
160+
# Handle double-encoded JSON objects.
161+
try:
162+
nested = json.loads(data)
163+
if isinstance(nested, dict):
164+
process_json_log_line(log_type, nested)
165+
parsed = True
166+
except json.JSONDecodeError:
167+
pass
168+
elif isinstance(data, list):
169+
# Handle a list of JSON records.
170+
if data and all(isinstance(item, dict) for item in data):
171+
for item in data:
172+
process_json_log_line(log_type, item)
173+
parsed = True
174+
except json.JSONDecodeError as e:
175+
# Handle concatenated JSON objects on a single line.
176+
if raw.startswith('{') and 'Extra data' in str(e):
177+
decoder = json.JSONDecoder()
178+
idx = 0
179+
while idx < len(raw):
180+
while idx < len(raw) and raw[idx].isspace():
181+
idx += 1
182+
if idx >= len(raw):
183+
break
184+
try:
185+
item, end = decoder.raw_decode(raw, idx)
186+
except json.JSONDecodeError:
187+
break
188+
if isinstance(item, dict):
189+
process_json_log_line(log_type, item)
190+
parsed = True
191+
idx = end
192+
193+
if not parsed:
160194
parts = raw.split('\t')
161195
process_text_log_line(log_type, parts)
162196
except Exception as e:

0 commit comments

Comments
 (0)