从jq json输出中排除列 [英] Exclude column from jq json output

查看:54
本文介绍了从jq json输出中排除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 jq timestamp 字段> JSON处理器.

I would like to get rid of the timestamp field here using jq JSON processor.

[
  {
    "timestamp": 1448369447295,
    "group": "employees",
    "uid": "elgalu"
  },
  {
    "timestamp": 1448369447296,
    "group": "employees",
    "uid": "mike"
  },
  {
    "timestamp": 1448369786667,
    "group": "services",
    "uid": "pacts"
  }
]

白名单也适用于我,即 select uid,group

White listing would also works for me, i.e. select uid, group

最终,我真正想要的是一个具有唯一值的列表,如下所示:

Ultimately what I would really like is a list with unique values like this:

employees,elgalu
employees,mike
services,pacts

推荐答案

如果只想删除时间戳,可以使用 del()函数:

If you just want to delete the timestamps you can use the del() function:

jq 'del(.[].timestamp)' input.json


但是要获得所需的输出,我不会使用 del()函数.由于您知道哪些字段应出现在输出中,因此您可以简单地使用 group id 填充数组,然后使用 join()函数:


However to achieve the desired output, I would not use the del() function. Since you know which fields should appear in output, you can simply populate an array with group and id and then use the join() function:

jq -r '.[]|[.group,.uid]|join(",")' input.json

-r 代表原始输出. jq 不会在值周围打印引号.

-r stands for raw ouput. jq will not print quotes around the values.

输出:

employees,elgalu
employees,mike
services,pacts

这篇关于从jq json输出中排除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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