Skip to content

Commit

Permalink
feat: Add hint to error message for extra struct field in JSON (#20612)
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion authored Jan 8, 2025
1 parent ccd130b commit 2a705b4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion crates/polars-json/src/json/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ fn deserialize_struct<'a, A: Borrow<BorrowedValue<'a>>>(

if let Some(v) = extra_field {
if !allow_extra_fields_in_struct {
polars_bail!(ComputeError: "extra key in struct data: {}", v)
polars_bail!(
ComputeError:
"extra field in struct data: {}, consider increasing infer_schema_length, or \
manually specifying the full schema to ignore extra fields",
v
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/io/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_write_json_decimal() -> None:
def test_json_infer_schema_length_11148() -> None:
response = [{"col1": 1}] * 2 + [{"col1": 1, "col2": 2}] * 1
with pytest.raises(
pl.exceptions.ComputeError, match="extra key in struct data: col2"
pl.exceptions.ComputeError, match="extra field in struct data: col2"
):
pl.read_json(json.dumps(response).encode(), infer_schema_length=2)

Expand Down Expand Up @@ -471,7 +471,7 @@ def test_read_json_raise_on_data_type_mismatch() -> None:


def test_read_json_struct_schema() -> None:
with pytest.raises(ComputeError, match="extra key in struct data: b"):
with pytest.raises(ComputeError, match="extra field in struct data: b"):
pl.read_json(
b"""\
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,7 @@ def test_json_decode_raise_on_data_type_mismatch_13061() -> None:


def test_json_decode_struct_schema() -> None:
with pytest.raises(ComputeError, match="extra key in struct data: b"):
with pytest.raises(ComputeError, match="extra field in struct data: b"):
pl.Series([r'{"a": 1}', r'{"a": 2, "b": 2}']).str.json_decode(
infer_schema_length=1
)
Expand Down

0 comments on commit 2a705b4

Please sign in to comment.