【问题标题】:How to select a JSON field contained in an array如何选择包含在数组中的 JSON 字段
【发布时间】:2021-03-12 19:55:21
【问题描述】:

您好,我有一个嵌套的 JSON 有效负载,我试图在一个 select 语句中从数组中选择一个字段,字段的结构如下: 但我想知道我是否可以在一个选择语句中做到这一点

|-- payload: struct (nullable = true)
 |    |-- detail: struct (nullable = true)
 |    |    |-- data: struct (nullable = true)
 |    |    |    |-- object: struct (nullable = true)
 |    |    |    |    |-- items: struct (nullable = true)
 |    |    |    |    |    |-- data: array (nullable = true)
 |    |    |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |    |    |-- billing_thresholds: string (nullable = true)
 |    |    |    |    |    |    |    |-- created: long (nullable = true)
 |    |    |    |    |    |    |    |-- id: string (nullable = true)
 |    |    |    |    |    |    |    |-- object: string (nullable = true)
 |    |    |    |    |    |    |    |-- plan: struct (nullable = true)
 |    |    |    |    |    |    |    |    |-- amount: long (nullable = true)             

我知道我可以做这样的事情,这会给我我需要的领域:

display(sT_customer_subscription_events
        .select($"payload.detail.data.object.items.data".getItems(0) as "item_data")
        .select($"item_data.plan.amount"
)

但我试图在第一个 select 语句中这样做,这对我下面不起作用

display(sT_customer_subscription_events.select($"payload.detail.data.object.items.data".getItems(0)."plan.month" )

【问题讨论】:

    标签: arrays json scala dataframe apache-spark-sql


    【解决方案1】:

    使用此查询:

    originalDF.withColumn("plan",
      from_json($"strJson", schema))
        .select($"plan.payload.detail.data.object.items.data"
          .getItem(0)
            .getField("plan")
              .getField("amount")).show()
    

    我得到结果:

    +-------------------------------------------------------------------+
    |plan.payload.detail.data.object.items.data AS `data`[0].plan.amount|
    +-------------------------------------------------------------------+
    |                                                                  0|
    +-------------------------------------------------------------------+
    

    看看这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-13
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      相关资源
      最近更新 更多