【问题标题】:parse multiline Json - array instead of row解析多行 Json - 数组而不是行
【发布时间】:2021-11-05 01:23:53
【问题描述】:

我有以下 json:

   {
      "data": {
        "data2": [],
        "data3": [
          {
            "info": {
              "test1": "value1",
              "test2": "value2"
            },
            "info2": [
              {
                "info": {
                  "test1": "value3",
                  "test2": "value4"
    },
          {
                "info": {
                  "test1": "value5",
                  "test2": "value6"
    }
              
            ]
          }
    ]
}}

我需要将 info2.info.test1、info2.info.test2 提取到列中。 我已经在火花中解析了它,但我有一个数组列而不是单行:

df = json.select(
      explode("data.data3.info2").alias("json"),
).select(
       col("json.info.test1"),
       col("json.info.test2")
)

预期输出:

| test1    | test2          |
| -------- | -------------- |
| value3   | value4         |
| value5   | value6         |

【问题讨论】:

  • 您能检查一下您提供的 JSON 文件吗?看起来它已经损坏了。顺便说一句,很高兴看到预期的输出。
  • 您的 json 似乎是错误的,请您仔细检查一下并告知预期的输出如何?
  • 我已添加更改,感谢 cmets。

标签: json apache-spark pyspark jsonparser


【解决方案1】:

我的工作解决方案:

df = json.select(
      explode("data.data3.info2").alias("json"),
).select(
      explode("json.info").alias("info")
).select(
       col("info.test1"),
       col("info.test2")
)

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多