json-repair 0.7.0__py3-none-any.whl → 0.8.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- json_repair/json_repair.py +6 -7
- {json_repair-0.7.0.dist-info → json_repair-0.8.1.dist-info}/METADATA +22 -19
- json_repair-0.8.1.dist-info/RECORD +7 -0
- json_repair-0.7.0.dist-info/RECORD +0 -7
- {json_repair-0.7.0.dist-info → json_repair-0.8.1.dist-info}/LICENSE +0 -0
- {json_repair-0.7.0.dist-info → json_repair-0.8.1.dist-info}/WHEEL +0 -0
- {json_repair-0.7.0.dist-info → json_repair-0.8.1.dist-info}/top_level.txt +0 -0
json_repair/json_repair.py
CHANGED
@@ -71,14 +71,10 @@ class JSONParser:
|
|
71
71
|
# This might be a <string> that is missing the starting '"'
|
72
72
|
elif char.isalpha():
|
73
73
|
return self.parse_string()
|
74
|
-
#
|
75
|
-
|
74
|
+
# If everything else fails, we just ignore and move on
|
75
|
+
else:
|
76
76
|
self.index += 1
|
77
|
-
self.skip_whitespaces_at()
|
78
77
|
return self.parse_json()
|
79
|
-
# If everything else fails, then we give up and return an exception
|
80
|
-
else:
|
81
|
-
raise ValueError("Invalid JSON format")
|
82
78
|
|
83
79
|
def parse_object(self) -> Dict[str, Any]:
|
84
80
|
# <object> ::= '{' [ <member> *(', ' <member>) ] '}' ; A sequence of 'members'
|
@@ -251,6 +247,9 @@ class JSONParser:
|
|
251
247
|
if number_str:
|
252
248
|
if "." in number_str or "e" in number_str or "E" in number_str:
|
253
249
|
return float(number_str)
|
250
|
+
elif number_str == "-":
|
251
|
+
# If there is a stray "-" this will throw an exception, throw away this character
|
252
|
+
return self.parse_json()
|
254
253
|
else:
|
255
254
|
return int(number_str)
|
256
255
|
else:
|
@@ -306,7 +305,7 @@ def repair_json(
|
|
306
305
|
It will return the fixed string by default.
|
307
306
|
When `return_objects=True` is passed, it will return the decoded data structure instead.
|
308
307
|
"""
|
309
|
-
json_str = json_str.strip().lstrip("```json")
|
308
|
+
json_str = json_str.strip().lstrip("```json")
|
310
309
|
parser = JSONParser(json_str)
|
311
310
|
if skip_json_loads:
|
312
311
|
parsed_json = parser.parse()
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: json_repair
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.8.1
|
4
4
|
Summary: A package to repair broken json strings
|
5
5
|
Author-email: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com>
|
6
6
|
License: MIT License
|
@@ -49,35 +49,28 @@ I searched for a lightweight python package that was able to reliably fix this p
|
|
49
49
|
|
50
50
|
# How to use
|
51
51
|
from json_repair import repair_json
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
# Not even this library could fix this JSON
|
52
|
+
|
53
|
+
good_json_string = repair_json(bad_json_string)
|
54
|
+
# If the string was super broken this will return an empty string
|
56
55
|
|
57
56
|
You can use this library to completely replace `json.loads()`:
|
58
57
|
|
59
58
|
import json_repair
|
60
|
-
|
61
|
-
|
62
|
-
except Exception:
|
63
|
-
# Not even this library could fix this JSON
|
59
|
+
|
60
|
+
decoded_object = json_repair.loads(json_string)
|
64
61
|
|
65
62
|
or just
|
66
63
|
|
67
64
|
import json_repair
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
## Performance considerations
|
65
|
+
|
66
|
+
decoded_object = json_repair.repair_json(json_string, return_objects=True)
|
67
|
+
|
68
|
+
### Performance considerations
|
74
69
|
If you find this library too slow because is using `json.loads()` you can skip that by passing `skip_json_loads=True` to `repair_json`. Like:
|
75
70
|
|
76
71
|
from json_repair import repair_json
|
77
|
-
|
78
|
-
|
79
|
-
except Exception:
|
80
|
-
# Not even this library could fix this JSON
|
72
|
+
|
73
|
+
good_json_string = repair_json(bad_json_string, skip_json_loads=True)
|
81
74
|
|
82
75
|
I made a choice of not using any fast json library to avoid having any external dependency, so that anybody can use it regardless of their stack.
|
83
76
|
|
@@ -85,6 +78,16 @@ Some rules of thumb to use:
|
|
85
78
|
- Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
|
86
79
|
- `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
|
87
80
|
|
81
|
+
## Adding to requirements
|
82
|
+
**Please pin this library only on the major version!**
|
83
|
+
|
84
|
+
We use TDD and strict semantic versioning, there will be frequent updates and no breaking changes in minor and patch versions.
|
85
|
+
To ensure that you only pin the major version of this library in your `requirements.txt`, specify the package name followed by the major version and a wildcard for minor and patch versions. For example:
|
86
|
+
|
87
|
+
json_repair==0.*
|
88
|
+
|
89
|
+
In this example, any version that starts with `0.` will be acceptable, allowing for updates on minor and patch versions.
|
90
|
+
|
88
91
|
# How it works
|
89
92
|
This module will parse the JSON file following the BNF definition:
|
90
93
|
|
@@ -0,0 +1,7 @@
|
|
1
|
+
json_repair/__init__.py,sha256=p9mZnte8Bg18NcxqgJ7vopH2gQv_XbZ0dRnk686QuRE,92
|
2
|
+
json_repair/json_repair.py,sha256=bngfZDyue6iwlsGIehK2QGfxN9XBM_rrR38lUe5jyVM,13263
|
3
|
+
json_repair-0.8.1.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
|
4
|
+
json_repair-0.8.1.dist-info/METADATA,sha256=-T0p-tSwV-_GoqK5d6DiAkSxUAPTgVQtXDIzv81ZSM8,6311
|
5
|
+
json_repair-0.8.1.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
6
|
+
json_repair-0.8.1.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
|
7
|
+
json_repair-0.8.1.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
json_repair/__init__.py,sha256=p9mZnte8Bg18NcxqgJ7vopH2gQv_XbZ0dRnk686QuRE,92
|
2
|
-
json_repair/json_repair.py,sha256=qoPNEOmXbsdByx_NRqQYEIc6lkKu5_i9tfuDox1utRc,13283
|
3
|
-
json_repair-0.7.0.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
|
4
|
-
json_repair-0.7.0.dist-info/METADATA,sha256=-xKlEHeXxS9lf75df7mcuiSalWpypZ8v4Ni3-xd-YvQ,6011
|
5
|
-
json_repair-0.7.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
6
|
-
json_repair-0.7.0.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
|
7
|
-
json_repair-0.7.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|