#8713·datasets

JSON loader turns strings like "123" or "null" into other types in inferred Json columns

Author: u7k4rs6Created Sep 30, 2026Updated Sep 30, 2026

When a JSON Lines column mixes objects and strings, load_dataset("json") infers Json() for it, and any string in that column that happens to be valid JSON comes back decoded.

For example rows {"a": {"b": 1}}, {"a": "123"}, {"a": "null"}, {"a": "true"}, {"a": "hello"} load back as {'b': 1}, 123, None, True, 'hello' instead of keeping the strings. Only "hello" survives because it isn't valid JSON.

It shows up with chat data where content is sometimes a string and sometimes a list of parts. With {"messages": [{"role": "user", "content": "what is 2+2"}, {"role": "assistant", "content": "4"}]} next to a row that has list content, the assistant answer comes back as the int 4.

The cause is json_encode_field assuming a string that parses as JSON is already encoded. That makes sense for user-declared features=Json(), where people pass pre-encoded strings, but on the inferred path the values were just decoded from the file, so a string there is always a real string.

I have a small fix that only changes the inferred path (when no features are passed) and leaves features=Json() as is. With it the example above round trips exactly, including through to_json and back, and in streaming mode. One thing it doesn't cover is re-encoding after a .map(), which still uses the same heuristic; changing that felt like your call. Happy to open the PR if the scoped approach sounds fine.

Tested on main (fa995bd).