使用JQ问题解析复杂的JSON文件 [英] Parsing Complex JSON file with JQ Issue

查看:116
本文介绍了使用JQ问题解析复杂的JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的JSON文件,结果为100000.我知道如何使用'JQ'进行基本解析.

I have big JSON file with 100000 results. I know how to do basic parsing with 'JQ'.

{
  "detected": true,
  "result": "Trojan.Win32.Generic!BT",
  "update": "20170115",
  "version": "1.5.0.42"
}
{
  "detected": true,
  "result": "FileCryptor.NJX",
  "update": "20170115",
  "version": "16.0.0.4749"
}
{
  "detected": true,
  "result": "Generic.Ransom.Purge.DC87C66E",
  "update": "20170115",
  "version": "1.0.1.9"
}

但是在这个JSON文件上,我想以CSV格式获取诸如"detected"和"result"之类的字段.我知道如何使用JQ单独获取它.

But on this JSON file , I would like to get the fields such as "detected" and "result" in a CSV format. I know how to get it individually using JQ.

我尝试过,

$ jq -r ".detected" virus.json 

true
true
true

2

 $ jq -r ".result" dum_1.json 

    Trojan.Win32.Generic!BT
    FileCryptor.NJX
    Generic.Ransom.Purge.DC87C66E

3

jq -r ".detected,.result" dum_1.json 
true
Trojan.Win32.Generic!BT
true
FileCryptor.NJX
true
Generic.Ransom.Purge.DC87C66E

而不是#3,我希望输出为

Instead of #3 , I would like the output to be as

true , Trojan.Win32.Generic!BT
true , FileCryptor.NJX
true , Generic.Ransom.Purge.DC87C66E

关于如何获得结果的任何建议?

Any suggestion on how to get the results ?

推荐答案

@csv将从平面数组转换为CSV,所以这应该使您入门:

@csv will convert from a flat array to CSV, so this should get you started:

jq -r '[.detected, .result] | @csv'

给出示例输入,将产生:

Given your sample input, this will produce:

true,"Trojan.Win32.Generic!BT"
true,"FileCryptor.NJX"
true,"Generic.Ransom.Purge.DC87C66E"

如果要删除引号,请考虑:

If you want the quotation marks removed, then consider:

jq -r '"\(.detected), \(.result)"'

这篇关于使用JQ问题解析复杂的JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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