如何使用jq流过滤JSON [英] How to filter JSON using jq stream

查看:178
本文介绍了如何使用jq流过滤JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的JSON文件,示例数据如下:

I have a very large JSON file and the sample data looks like as:

{"userActivities":{"L3ATRosRdbDgSmX75Z":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-19"},"L3ATSFGrpAYRkIIKqrh":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-20"}}}

我需要在jq流中的"localDate"字段上放置一个过滤器,以使输出看起来像以下JSON:

I need to put a filter on "localDate" field in jq stream such that the output looks like the following JSON as:

{"L3ATSFGrpAYRkIIKqrh":{"deviceId":"60ee32c2fae8dcf0","dow":"Friday","localDate":"2018-01-19"}}

非常感谢您的帮助/指导!

Any help/guidance is greatly appreciated!

推荐答案

在基于值进行选择时保留键值对的最简单方法是使用with_entries:

The simplest way to retain the key-value pair while making a selection based on the values is to use with_entries:

jq '.userActivities
| with_entries(select(.value.localDate=="2018-01-20"))' input.json

输出

{
  "L3ATSFGrpAYRkIIKqrh": {
    "deviceId": "60ee32c2fae8dcf0",
    "dow": "Friday",
    "localDate": "2018-01-20"
  }
}

这篇关于如何使用jq流过滤JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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