【问题标题】:including $ in search string for get_json_object在 get_json_object 的搜索字符串中包括 $
【发布时间】:2019-08-23 20:44:02
【问题描述】:

$ 用作get_json_object 的根对象。我的 json 字符串在 json 键的名称中已经有 $,我怎样才能提取它的值?我不想使用 json_tuple。

create external table testing_hive (records string);
insert into testing_hive values("{\"$num\":\"hey\"}");

select get_json_object(testing_hive.records, '$.$num') from testing_hive;

【问题讨论】:

  • 请问不使用json_tuple是什么原因?
  • 基本上json太嵌套,要避免每次都使用横向视图。但是,最终使用了 json_tuple

标签: json hive hiveql


【解决方案1】:

您可以将"$num" 替换为不包含$ 的其他内容,例如"xx_num"

select get_json_object(regexp_replace(testing_hive.records,'\\"\\$num\\"','\\"xx_num\\"'), '$.xx_num') as num from testing_hive;

结果:

hey

您还可以在单​​个 regex_replace 中将所有键的 $ 替换为其他前缀:

regexp_replace(testing_hive.records,'\\"\\$(.*?\\":)','\\"xx_$1')

我在模式中包含": 以确保它只匹配键,而不是值。如果您想删除$ 并保留没有$ 的密钥,请使用'$1' 代替'\\"xx_$1'

希望你明白了。相应地修改正则表达式模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多