使用jq获取具有最新时间戳的json对象 [英] Get json object that has latest timestamp using jq

查看:55
本文介绍了使用jq获取具有最新时间戳的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个低于json的文件,但我一直在努力只显示最新的createdDatedescription.

I have a below json file but I'm struggling to only display the description with the latest createdDate.

我尝试过

>
<
todateiso8601?
now

还有一些,但我无法使它正常工作.

and a few more but I can't get this to work.

任何人都可以提供帮助吗?

Would anyone be able to help?

JSON:

{
    "items": [
        {
            "createdDate": 1543585940, 
            "id": "awefef", 
            "description": "this is description 1"
        }, 
        {
            "createdDate": 1555324487, 
            "id": "hjkvhuk", 
            "description": "this is description 2"
        }, 
        {
            "createdDate": 1547034297, 
            "id": "xdfxdfv", 
            "description": "this is description 3"
        }
]
}

推荐答案

只需按.createdDate排序,然后(假设即使只有多个.createdDate值最大,也只需要一个值),请选择最后一个:

Simply sort by .createdDate and (assuming you only want one value even if there is more than one with the greatest .createdDate value), select the last one:

.items
| sort_by(.createdDate)[-1].description

领带

如果需要平局中的所有描述:

Ties

If you want all the descriptions in the case of ties:

.items
| sort_by(.createdDate)
| (.[-1].createdDate) as $max
| .[]
| select($max == .createdDate)
| .description

这篇关于使用jq获取具有最新时间戳的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆