使用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)"'