你如何在点语法中转义点在get_json_object中? [英] How do you escape dot in dot syntax e.g. in get_json_object?

查看:116
本文介绍了你如何在点语法中转义点在get_json_object中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取名称中带点的属性值.我正在努力逃脱这个点.

I need to get the value of an attribute with a dot in it's name. I'm struggling to escape the dot.

get_json_object($"AColumn", "$.something.id")

我已经尝试过"$. something.id ","$.['something.id']","$.something`.id";而且这些似乎都不起作用.

I've tried "$.something.id", "$.['something.id']", "$.something`.id" and none of these seem to work.

不幸的是,以前有人成功做到过吗?有人可以说明情况吗?

Has anyone successfully done this before? Could anyone shed some light on the situation?

推荐答案

尝试一下-

   val df = Seq(("p1", """{"a.id": 1, "b": 2}"""), ("p2", """{"a.id": 3}"""))
      .toDF("p_id", "p_meta")
    df.show(false)
    df.printSchema()
    /**
      * +----+-------------------+
      * |p_id|p_meta             |
      * +----+-------------------+
      * |p1  |{"a.id": 1, "b": 2}|
      * |p2  |{"a.id": 3}        |
      * +----+-------------------+
      *
      * root
      * |-- p_id: string (nullable = true)
      * |-- p_meta: string (nullable = true)
      */

按以下方式使用 get_json_object


    df.withColumn("x", get_json_object($"p_meta", "$['a.id']"))
      .show(false)
    /**
      * +----+-------------------+---+
      * |p_id|p_meta             |x  |
      * +----+-------------------+---+
      * |p1  |{"a.id": 1, "b": 2}|1  |
      * |p2  |{"a.id": 3}        |3  |
      * +----+-------------------+---+
      */

您还可以使用 json_tuple


    df.withColumn("x", json_tuple($"p_meta", "a.id"))
      .show(false)
    /**
      * +----+-------------------+---+
      * |p_id|p_meta             |x  |
      * +----+-------------------+---+
      * |p1  |{"a.id": 1, "b": 2}|1  |
      * |p2  |{"a.id": 3}        |3  |
      * +----+-------------------+---+
      */

这篇关于你如何在点语法中转义点在get_json_object中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆