Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/datachain/lib/meta_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ def gen_datamodel_code(
json_object = json_object[0] # sample the 1st object from JSON array
if format == "jsonl":
format = "json" # treat json line as plain JSON in auto-schema

from datachain.lib.utils import normalize_col_names

column_mapping = normalize_col_names(list(json_object.keys()))
json_object = {
new_key: json_object[old_key] for new_key, old_key in column_mapping.items()
}

data_string = json.dumps(json_object)

import datamodel_code_generator
Expand Down
34 changes: 34 additions & 0 deletions tests/func/test_mutate.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,37 @@ def test_mutate_reject_new_nested_columns(test_session):
dc.read_values(file=files, session=test_session).mutate(
something__new=Column("file.source")
).save("test_reject_nested")


def test_mutate_column_skips_double_quoting():
import tempfile

with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
f.write('[{"xy": 0}]')
temp_path = f.name

chain = dc.read_json(temp_path)
chain = chain.mutate(out=dc.C("json.xy"))
df = chain.to_pandas()
assert df.columns.tolist() == [("json", "xy"), ("out", "")]

with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
f.write('[{"xY": 0}]')
temp_path = f.name
chain = dc.read_json(temp_path)

# `xY` column is not found as it was sanitised
with pytest.raises(SignalResolvingError):
chain = chain.mutate(out=dc.C("json.xY"))

chain = chain.mutate(out=dc.C("json.xy"))
df = chain.to_pandas()
assert df.columns.tolist() == [("json", "xy"), ("out", "")]

with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
f.write('[{"x_y": 0}]')
temp_path = f.name
chain = dc.read_json(temp_path)
chain = chain.mutate(out=dc.C("json.x_y"))
df = chain.to_pandas()
assert df.columns.tolist() == [("json", "x_y"), ("out", "")]