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

オープン
#41,098 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
371

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/beam のほかの issue

apache/beam の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。