Skip to content

fix(json): write valid JSON array when lines=False with batching#8275

Open
KirtiRamchandani wants to merge 1 commit into
huggingface:mainfrom
KirtiRamchandani:fix/to-json-lines-false-batched-7037
Open

fix(json): write valid JSON array when lines=False with batching#8275
KirtiRamchandani wants to merge 1 commit into
huggingface:mainfrom
KirtiRamchandani:fix/to-json-lines-false-batched-7037

Conversation

@KirtiRamchandani

Copy link
Copy Markdown

Summary

  • Fix Dataset.to_json(lines=False, orient="records") when the dataset is larger than batch_size
  • Previously each batch wrote a separate top-level JSON array ([...][...]), producing invalid JSON (JSONDecodeError: Extra data)
  • Merge batched pandas outputs into one array by stripping per-batch brackets and joining with commas

Fixes #7037

Test plan

  • pytest tests/io/test_json.py::TestJsonDatasetWriter::test_dataset_to_json_records_batched_without_lines
  • pytest tests/io/test_json.py::TestJsonDatasetWriter::test_dataset_to_json_lines tests/io/test_json.py::TestJsonDatasetWriter::test_dataset_to_json_orient

/cc @albertvillanova

Batched exports with lines=False previously wrote multiple top-level JSON
arrays back-to-back, producing invalid JSON for datasets larger than
batch_size. Merge batch outputs into a single array for orient=records.

Fixes huggingface#7037
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.

A bug of Dataset.to_json() function

1 participant