Skip to content

Load mixed-type JSON fields declared as strings without quoting them - #9149

Open
behroozazarkhalili wants to merge 2 commits into
huggingface:mainfrom
behroozazarkhalili:fix/7496-json-declared-string-fields
Open

behroozazarkhalili wants to merge 2 commits into
huggingface:mainfrom
behroozazarkhalili:fix/7496-json-declared-string-fields

Conversation

@behroozazarkhalili

@behroozazarkhalili behroozazarkhalili commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7496

Since the JSON loader falls back to Json() for mixed-type fields, the Arrow error from the issue doesn't happen anymore, but the requested feature type is still applied after the field was converted. If a field has mixed types and is declared as a string in features, the loader encodes it as JSON text and then casts that text to the string column, so string values come back quoted:

# a.jsonl: {"a": 1} / {"a": "x"}
load_dataset("json", data_files="a.jsonl", features=Features({"a": Value("string")}), split="train")["a"]
# main:    ['1', '"x"']
# this PR: ['1', 'x']

As suggested by @lhoestq I first tried to pass an Arrow schema to paj.read_json, but with explicit_schema a string field still fails on numbers (Column(/a) changed from string to number), so the values have to be converted before parsing.

For the fields declared as strings (Value("string") or Value("large_string"), including in structs and lists), when Arrow reports that their type changes, the values that are not strings are replaced by their JSON text and the strings are kept (string_encode_field in utils/json.py). This uses the standard json module (with compact separators, like Json() fields), since ujson keeps only 10 significant digits: 0.1 + 0.2 stays "0.30000000000000004" and big integers keep all their digits. The other mixed fields still use Json(). Like the Json() fallback, this only happens for a field once Arrow reports a type change, and the next batches are encoded directly. It uses the features given by the user (self.config.features), and also works with on_mixed_types=None.

Tests (new functions in tests/packaged_modules/test_json.py): int/float/bool/object/null values mixed with strings, a string that is valid JSON text, a list of strings, a field in a struct, .jsonl and .json, on_mixed_types="use_json" and None; a declared string field next to a Json() field; all the digits of floats and big integers, without changing the other columns (JSON Lines); and a file with many batches, where only the first batch with mixed types is parsed twice. On main 25 of the 27 new tests fail (the 2 that pass are the "valid JSON text" case, which already worked). tests/packaged_modules/test_json.py, tests/io/test_json.py, tests/features and tests/test_load.py on Python 3.12: 734 passed; the only failure, test_load_dataset_builder_then_edit_then_load_again, also fails on main on our Lustre file system. On Python 3.10 the new tests fail with the xPath error of main that #9119 fixes, like the other tests that load local JSON files.

Known differences, not changed here: a float that ends with .0 is "1" in a batch where the field only has numbers (Arrow's cast) and "1.0" in a batch where it is mixed with strings; and in .json files (not JSON Lines) the whole file is first parsed and re-encoded with ujson, which already rounds floats to 10 digits on main (e.g. 0.30000000000000004 is loaded as 0.3, in any column) and doesn't support integers above 2**64.

This conflicts with #8928 (strings that are valid JSON in Json() fields) in two places of json.py, where both PRs change how a batch is re-encoded. I merged the two branches locally: keeping both changes resolves the conflicts, and the JSON tests of both PRs pass on the result, so whichever is merged second only needs that merge.

When a field has mixed types, the JSON loader re-encodes it as JSON text
and loads it as Json(). If the user declared the field as a string in
`features`, that JSON text was cast to the string column as is, so a
string value "x" came back as '"x"'.

For the fields declared as strings, only the values that are not strings
are now replaced by their JSON text before Arrow parses the batch, and
the strings are kept. This also works with on_mixed_types=None, and the
field is encoded in the next batches without parsing them twice.

Fixes huggingface#7496
ujson keeps 10 significant digits, so a float like 0.1 + 0.2 in a
mixed-type field declared as a string became "0.3", and the other float
columns of the batch were rounded too when the batch was re-encoded.
Encode the declared string fields with the standard json module (compact
separators, like the Json() fields), and only re-encode the Json() fields
with ujson when there are some.
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.

Json builder: Allow features to override problematic Arrow types

1 participant