You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Snowpark DataFrame中提取JSON列的指定嵌套字段?

在Snowpark DataFrame中提取JSON嵌套字段的方法

直接将JSON路径字符串传给select()会失效,因为Snowpark会把它当作普通列名而非JSON路径解析。你可以通过以下几种方式实现,无需依赖session.sql():

  • 使用col()函数配合Snowflake风格的路径表达式
    用col()包裹JSON路径,让Snowpark识别这是对JSON列的嵌套字段访问:

    from snowflake.snowpark.functions import col
    
    df_selected = df.select(col("col3:Construction_method:CAD:designer").alias("designer"))
    
  • 使用get_path()方法指定嵌套路径
    通过get_path()传入路径列表,精准定位嵌套字段:

    from snowflake.snowpark.functions import col
    
    df_selected = df.select(
        col("col3").get_path(["Construction_method", "CAD", "designer"]).alias("designer")
    )
    
  • 逐层调用get()方法访问
    适合层级较多的场景,可读性更强:

    from snowflake.snowpark.functions import col
    
    df_selected = df.select(
        col("col3")
        .get("Construction_method")
        .get("CAD")
        .get("designer")
        .alias("designer")
    )
    

以上三种方式都能直接在已创建的DataFrame上操作,无需切换到原生SQL。

内容的提问来源于stack exchange,提问作者CodingNewbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:52:04