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

相关推荐
泡泡鱼(敲代码中)4 小时前
Python 字符串 str 完整学习笔记
python·学习
kyrie_sakura5 小时前
python学习笔记15 -- Anaconda,Jupyter,numpy
python·学习·jupyter·numpy
蓝悦无人机7 小时前
LangChain v1.0 系列教程——第2章 工具系统
langchain·json·装饰器模式·pydantic
学代码的CJY7 小时前
CJY 知识工作台:博客写作使用指南
数据仓库·知识图谱
2601_962680227 小时前
数据分析面试常考的SQL题和业务题分别是哪几类?
sql·面试·数据分析
PC2005-cloud7 小时前
Rust学习笔记:控制流——if、loop、while、for与match
笔记·学习·rust
彧azz8 小时前
DFS与BFS:图遍历的两大核心算法
数据结构·学习·算法·深度优先·广度优先
kyrie_sakura8 小时前
python学习笔记14 -- FastAPI
笔记·python·学习·fastapi
子非鱼a8 小时前
【SQL注入】 ShadowCart
sql
来两个炸鸡腿9 小时前
【Datawhale2609】算子开发实战 task01-熟悉沐曦GPU
人工智能·学习·大模型