Skip to content

Commit

Permalink
Release 0.23.0, support multiple json objects in one string, will ret…
Browse files Browse the repository at this point in the history
…urn an array of repaired json objects
  • Loading branch information
mangiucugna committed Jun 2, 2024
1 parent 753fe67 commit d40dec8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "json_repair"
version = "0.22.0"
version = "0.23.0"
license = {file = "LICENSE"}
authors = [
{ name="Stefano Baccianella", email="[email protected]" },
Expand Down
22 changes: 20 additions & 2 deletions src/json_repair/json_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,25 @@ def __init__(
def parse(
self,
) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]:
json = self.parse_json()
if self.index < len(self.json_str):
json = [json]
last_index = self.index
while self.index < len(self.json_str):
j = self.parse_json()
if j != "":
json.append(j)
if self.index == last_index:
self.index += 1
last_index = self.index
if len(json) == 1:
json = json[0]
elif len(json) == 0:
json = ""
if self.logger.log_level == "none":
return self.parse_json()
return json
else:
return self.parse_json(), self.logger.log
return json, self.logger.log

def parse_json(
self,
Expand Down Expand Up @@ -650,3 +665,6 @@ def from_file(
fd.close()

return jsonobj


repair_json("[]{}")
5 changes: 5 additions & 0 deletions tests/test_json_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def test_leading_trailing_characters():
```json
{ "key": "value" }
```""") == '{"key": "value"}'
def test_multiple_jsons():
assert repair_json("[]{}") == "[[], {}]"
assert repair_json("{}[]") == "[{}, []]"
assert repair_json('{"key":"value"}[1,2,3,True]') == '[{"key": "value"}, ["1,2,3", true]]'
assert repair_json('lorem ```json {"key":"value"} ``` ipsum ```json [1,2,3,True] ``` 42') == '[{"key": "value"}, ["1,2,3", true]]'

def test_repair_json_with_objects():
# Test with valid JSON strings
Expand Down

0 comments on commit d40dec8

Please sign in to comment.