Hive学习(14)json解析get_json_object()函数

一、语法

目的:在一个标准JSON字符串中,按照指定方式抽取指定的字符串。

string get_json_object(string <json>, string <path>)

参数说明

  1. json:必填。STRING类型。标准的JSON格式对象,格式为{Key:Value, Key:Value,...}。如果遇到英文双引号("),需要用两个反斜杠(\)进行转义。如果遇到英文单引号('),需要用一个反斜杠(\)进行转义。
  2. path:必填。STRING类型。表示在json中的path,以$开头。
  3. $:表示根节点。
  4. .或'':表示子节点。
  5. *:返回整个数组。

二、例:在数据表test中有一列jsonData,其数据如下:

复制代码
jsonData = '{
	"ID":121,
	"message":{
		"name":"Asher",
		"location":[{"county":"浦东","city":"上海"},
					{"county":"西直门","city":"北京"}]
	}	
}'
  1. 提取jsonData第一层数据ID

    select get_json_object(jsonData,'$.ID') from test

    --输出

    121

  2. 提取jsonData第二层数据name

    select get_json_object(jsonData,'$.message.name') from test

    --输出

    Asher

  3. 提取jsonData第二层数据location的第一项

    select get_json_object(jsonData,'$.message.location[0]') from test

    --输出

    {"county":"浦东","city":"上海"}

  4. 提取jsonData第二层数据location的第一项的city

    select get_json_object(jsonData,'$.message.location[0].city') from test

    --输出

    上海

5.提取jsonData第三层数据city

复制代码
select get_json_object(jsonData,'$.message.location.city') from test

--输出
>["上海","北京"]

码字不易,喜欢请点赞,谢谢!!!😊

参考:

https://blog.csdn.net/Asher117/article/details/107984650

https://help.aliyun.com/zh/maxcompute/user-guide/get-json-object

相关推荐
li星野1 小时前
5 TYPES OF ADVERBS — One-Page Notes
学习
星恒随风1 小时前
Linux库的底层原理:ELF文件格式、静态链接、程序加载、虚拟地址空间与GOT/PLT详解
linux·运维·笔记·学习·状态模式
罗西的思考1 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 — (7)— 混合机制
笔记·学习
starsky762382 小时前
Agent学习——上下文工程(三)
学习
这个DBA有点耶2 小时前
PostgreSQL HOT更新:频繁更新表的索引膨胀根因与fillfactor调优
数据库·sql·postgresql
xier_ran3 小时前
【infra之路】AI 编译器学习笔记(四):PatternRewriter 与贪心 driver
人工智能·笔记·学习
小弥儿4 小时前
GitHub今日热榜 | 2026-10-12:pytorch 上升四位
人工智能·pytorch·学习·开源·github
AOI小白新手上路4 小时前
DeepLens 的课程学习自动设计与同源仿真论文综述
学习
一条小小yu4 小时前
Text-to-SQL Agent
数据库·sql
老王爱玩车4 小时前
预处理详解
c语言·开发语言·学习