json - 配置单元:解析 JSON 包含\"

标签 json hadoop hive hiveql

我的 JSON 数据是:

 {
    "content":"{\"type\":3,\"from\":\"home\"}",
    "id":"239",
    "idtype":"0",
    "timestamp":"1547957367281",
    "type":"0"
}

我想将其放入以下格式的表 json_data 中:

+-------------+  
| from        |  
+-------------+  
| home        |  
+-------------+

我如何使用此处的爆炸函数来获得所需的输出?

最佳答案

您可以使用 regexp_replace 删除 \,同时 { 之前和 之后的 " 也应该被删除。使用 get_json_objectjson_tuple 提取属性。在您的数据示例上测试它:

select get_json_object(json,'$.content.from') as `from`
from
(
select
regexp_replace(
regexp_replace(
regexp_replace(
'{"content":"{\"type\":3,\"from\":\"home\"}",
    "id":"239",
    "idtype":"0",
    "timestamp":"1547957367281",
    "type":"0"
}'               --original data
,'\\\"','\"')    --replace \" with "
,'\\"\\{','{')   --remove " before {
,'\\}\\"','\\}') --remove " after } --these last two can be combined
as json
)s
;

输出:

OK
from
home
Time taken: 0.329 seconds, Fetched: 1 row(s)

最好逐一检查这些 regexp_replaces 以确保它按预期工作。希望你明白了

关于json - 配置单元:解析 JSON 包含\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289524/

相关文章:

javascript - 如何向此 Javascript 添加 if/else 语句?

java - JPA:可以使用 "long fooId"而不是 "Foo foo"作为 @JoinColumn (对于 JSON 兼容)?

hadoop - 使用 mapreduce 修改 hdfs 上的文件

hadoop - PIG 拉丁语 : While loading how to discard the first line in any file?

hadoop - 需要清晰了解hiveconf并在hive中设置命令

java - 读取具有多个属性的 JSON 文件

ios - 我无法从 json 解析某个值

hadoop - HCatalog:线程 “main” java.lang.IncompatibleClassChangeError中的异常

r - 在已安装 Hadoop 的集群上使用 R

java - Hive GUDF 自定义解压缩功能测试用例失败,错误为 "B cannot be cast to org.apache.hadoop.io.BytesWritable"