[Bug]: [Python] ReadFromJson stores nested JSON objects and arrays as Python repr strings
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 70/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- pandas, python
- Domain
- data-engineering
Research direction
Start in sdks/python/apache_beam/dataframe/io.py, in ReadViaPandas.expand, where object-dtype columns are cast with astype(pd.StringDtype()). Run the reproducer from the issue to see the nested dict and list values come out as Python repr. Done when nested objects and arrays read from JSON are stored as valid JSON strings, and a regression test in textio_test.JsonTest covers this.
Written by the indexing model from the issue text.
Description
What happened?
beam.io.ReadFromJson (and therefore Beam YAML ReadFromJson) reads nested JSON objects and arrays as strings holding their Python repr instead of JSON. For the line {"id": 1, "user": {"name": "a"}, "tags": ["x", "y"]} the element is id=1, user="{'name': 'a'}", tags="['x', 'y']". The single-quoted repr is not valid JSON, so downstream steps can't recover the nested values, and the schema reports the field as Optional[str].
Minimal reproducer:
import json, os, tempfile
import apache_beam as beam
with tempfile.TemporaryDirectory() as d:
path = os.path.join(d, 'nested.jsonl')
with open(path, 'w') as f:
f.write('{"id": 1, "user": {"name": "a"}, "tags": ["x", "y"]}\n')
with beam.Pipeline() as p:
(p
| beam.io.ReadFromJson(path)
| beam.Map(lambda r: print(repr(r.user), json.loads(r.user))))
# prints "{'name': 'a'}" and then fails with json.decoder.JSONDecodeError
Cause
ReadFromJson delegates to apache_beam.dataframe.io.ReadViaPandas('json', ...). With the default objects_as_strings=True, ReadViaPandas.expand (sdks/python/apache_beam/dataframe/io.py) casts every object-dtype column with df[col].astype(pd.StringDtype()). For JSON input, pandas keeps nested objects and arrays as dict / list values in object-dtype columns, so the cast stores str(dict).
Encoding dict / list values with json.dumps before the cast (JSON format only) keeps the column a string but makes it valid JSON. I have a small fix with a regression test in textio_test.JsonTest and can open a PR.
Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
Issue Components
- Component: Python SDK
- Component: Java SDK
- Component: Go SDK
- Component: Typescript SDK
- Component: IO connector
- Component: Beam YAML
- Component: Beam examples
- Component: Beam playground
- Component: Beam katas
- Component: Website
- Component: Infrastructure
- Component: Spark Runner
- Component: Flink Runner
- Component: Prism Runner
- Component: Hazelcast Jet Runner
- Component: Google Cloud Dataflow Runner
- Dominant language
- Java
- Stars
- 8.7k
- Forks
- 4.8k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 368
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/beam
-
awaiting triage bug P3
Difficulty 1/5 Under an hour Newbie friendliness 67/100
apache/beam#41197 · 2 comments ·
Maintainers usually reply within 1 day
-
[Bug]: [Python] WriteToJson silently rounds floats to 10 decimal placesPossibly taken @Abhi2939 claimed this 1 day ago. Openio P2 python yaml
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
apache/beam#41102 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
apache/beam#39624 · 2 reactions ·
Maintainers usually reply within 1 day
-
[Bug]: PubsubIO used in batch incorrect batch cutoff sizePossibly taken @1fanwang claimed this 78 days ago. Openbug io P3 pinned pubsub
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
apache/beam#28011 · 4 comments ·
Maintainers usually reply within 1 day
-
build P3 sub-task
Difficulty 1/5 Under an hour Newbie friendliness 68/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
SasanLabs/VulnerableApp#912 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
desktop edt not responding
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
datafusion-contrib/StreamFusion#653 ·
Maintainers usually reply within 1 day
-
[Bug] Absolute row/column index 0 is ignored for images, hyperlinks and commentsPossibly taken @fanOfSunny claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day