From efcb53e062b6112a100d2b40511224d4e8067a29 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 21 Oct 2024 18:39:31 +0200 Subject: [PATCH] repair bad jsons --- evidence/parse.py | 9 ++++++++- evidence/parse_details.py | 8 +++++++- requirements.txt | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/evidence/parse.py b/evidence/parse.py index fa0903c..8688d6f 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -5,6 +5,8 @@ import hashlib from datetime import datetime from dmidecode import DMIParse +from json_repair import repair_json + from evidence.models import Annotation from evidence.xapian import index from utils.constants import ALGOS, CHASSIS_DH @@ -20,7 +22,12 @@ def get_network_cards(child, nets): def get_mac(lshw): nets = [] try: - get_network_cards(json.loads(lshw), nets) + hw = json.loads(lshw) + except json.decoder.JSONDecodeError: + hw = json.loads(repair_json(lshw)) + + try: + get_network_cards(hw, nets) except Exception as ss: print("WARNING!! {}".format(ss)) return diff --git a/evidence/parse_details.py b/evidence/parse_details.py index fd7f1c2..57ce4f9 100644 --- a/evidence/parse_details.py +++ b/evidence/parse_details.py @@ -3,6 +3,8 @@ import numpy as np from datetime import datetime from dmidecode import DMIParse +from json_repair import repair_json + from utils.constants import CHASSIS_DH, DATASTORAGEINTERFACE @@ -480,7 +482,11 @@ class ParseSnapshot: def loads(self, x): if isinstance(x, str): try: - return json.loads(x) + try: + hw = json.loads(lshw) + except json.decoder.JSONDecodeError: + hw = json.loads(repair_json(lshw)) + return hw except Exception as ss: print("WARNING!! {}".format(ss)) return {} diff --git a/requirements.txt b/requirements.txt index 1576d4f..32b8e86 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,4 @@ pandas==2.2.2 xlrd==2.0.1 odfpy==1.4.1 pytz==2024.2 - +json-repair==0.30.0