[Bug]: [Python] ReadFromJson stores nested JSON objects and arrays as Python repr strings

未关闭
#41,098 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
半天
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
pandas, python

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

awaiting triage bug io P2 yaml
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
主要语言
Java
星标
8.7k
派生
4.8k
平均合并
2 天 12 小时
30 天内合并 PR
371

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/beam 的其他 Issue

查看 apache/beam 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。