Hive SQL:访问 Json 数组的最后一个元素

发布于 2025-01-16 07:58:57 字数 474 浏览 3 评论 0原文

我有一个名为“schedule_info”的 json 列,其格式如下:

{schedule: [{"date":"2020-01-01"},{"date":"2021-01-01"},{"date":"2022-01-01"},{"date":"2022 -02-01"},{"date":"2022-03-01"}]}

我可以通过以下方式轻松访问第一个元素:

get_json_object(schedule_info, '$.schedule[0].date') 作为first_date。

但是,当我尝试访问最后一个日期时,我无法..

我尝试过“get_json_object(schedule_info, '$.schedule[-1].date')”。但是,这仅返回整个计划数组。我也尝试过将数组长度包含在括号中,但效果不佳。有没有一种简单的方法可以做到这一点?

我想轻松地从 json 数组的最后一个元素解析出数据,而不必分解数组。

I have a json column called "schedule_info" with a format like the following:

{schedule: [{"date":"2020-01-01"},{"date":"2021-01-01"},{"date":"2022-01-01"},{"date":"2022-02-01"},{"date":"2022-03-01"}]}

I'm able to access the first element easily with:

get_json_object(schedule_info, '$.schedule[0].date') as first_date.

However, When I try to access the last date, I'm not able to..

I've tried "get_json_object(schedule_info, '$.schedule[-1].date')". But, this just returns the whole schedule array. I've also tried including the array length in the brackets, which didn't work as well.. Is there an easy way to do this?

I want to easily parse out data from the last element of a json array without having to explode the array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2025-01-23 07:58:57

如果您使用brickhouse,json_split可以帮助您:

select 
json_split(get_json_object(schedule_info, '$.schedule'))[
size(json_split(get_json_object(schedule_info, '$. schedule')))-1
] as last_date
from your_table

这里您使用json_split两次,一次用于获取数组元素,另一次用于获取数组大小

或使用reverse< /code> 消除对 json_split 的一次调用

select 
reverse(json_split(get_json_object(schedule_info, '$.schedule')))[0] as last_date
from your_table

json_split 博客

brickhouse github

if you use brickhouse, json_split could help you like this:

select 
json_split(get_json_object(schedule_info, '$.schedule'))[
size(json_split(get_json_object(schedule_info, '$. schedule')))-1
] as last_date
from your_table

here you use json_split twice, one for get array element, another for get array size

or use reverse to eliminate one call to json_split

select 
reverse(json_split(get_json_object(schedule_info, '$.schedule')))[0] as last_date
from your_table

json_split blog

brickhouse github

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文