Skip to content

fix: respect string literals when splitting JSON objects#2015

Open
JSap0914 wants to merge 1 commit into
unclecode:developfrom
JSap0914:fix/split-json-objects-brace-in-string
Open

fix: respect string literals when splitting JSON objects#2015
JSap0914 wants to merge 1 commit into
unclecode:developfrom
JSap0914:fix/split-json-objects-brace-in-string

Conversation

@JSap0914

Copy link
Copy Markdown

Summary

split_and_parse_json_objects splits a JSON array string into individual
object segments by tracking brace ({/}) depth. The scan counted every
brace, including those that appear inside JSON string values. A value such as
"x}y" therefore terminated the object early, so the resulting segment was
invalid JSON, failed to parse, and the valid objects were silently dropped.

Example (before this change):

>>> split_and_parse_json_objects('[{"a": "x}y"}, {"b": 2}]')
([], ['{"a": "x}'])

Both objects are lost even though the input is valid JSON.

The fix tracks string state (and backslash escapes) so braces inside string
values no longer affect the depth count:

>>> split_and_parse_json_objects('[{"a": "x}y"}, {"b": 2}]')
([{'a': 'x}y'}, {'b': 2}], [])

This helper is used to recover individual objects from LLM extraction output
(extraction_strategy.py), where string values containing braces are common.

List of files changed and why

  • crawl4ai/utils.py - make the brace scanner in split_and_parse_json_objects string-aware (skip braces inside strings, honor \ escapes).
  • tests/general/test_split_and_parse_json_objects.py - new regression tests covering closing/opening braces and escaped quotes inside string values, plus plain/balanced cases.

How Has This Been Tested?

$ python -m pytest tests/general/test_split_and_parse_json_objects.py tests/regression/test_reg_utils.py -q
76 passed in 2.07s

The three new cases fail on develop (objects dropped) and pass with this change; the existing tests/regression/test_reg_utils.py suite continues to pass.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added/updated unit tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

split_and_parse_json_objects counted every { and } to find object
boundaries, including braces inside JSON string values. A value such as
"x}y" terminated the object early, so the segment failed to parse and
valid objects were silently dropped.

Track string state (and backslash escapes) so braces inside string
values no longer affect the depth count.

Add regression tests covering closing/opening braces and escaped quotes
inside string values.
Copilot AI review requested due to automatic review settings June 16, 2026 04:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens split_and_parse_json_objects so it won’t mis-split JSON objects when braces appear inside JSON string values, and adds regression tests to prevent recurrence.

Changes:

  • Track string/escape state while scanning to ignore {/} inside JSON string values.
  • Add pytest regression cases covering braces inside string values and escaped quotes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
tests/general/test_split_and_parse_json_objects.py Adds regression tests for braces/quotes inside JSON strings.
crawl4ai/utils.py Updates splitting logic to ignore braces while inside JSON strings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crawl4ai/utils.py
Comment on lines +727 to +740
in_string = False
escape = False

for i, char in enumerate(json_string):
# Skip the character escaped by a preceding backslash (e.g. \" or \\).
if escape:
escape = False
continue
if char == "\\":
escape = True
continue
if char == '"':
in_string = not in_string
continue
assert unparsed == []


def test_balanced_braces_inside_string_still_work():
assert unparsed == []


def test_escaped_quote_inside_string_value():
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants