I need to extract a particular anti-virus product scan results from a JSON file and list the elements in csv format for further processing. JSON file sample as below:
Contents of a file scanresults.json
{
"scans": {
"Bkav": {
"detected": true,
"version": "1.3.0.9899",
"result": "W32.AIDetect.malware2",
"update": "20230417"
},
"Lionic": {
"detected": true,
"version": "7.5",
"result": "Trojan.Win32.Generic.4!c",
"update": "20230417"
},
"Elastic": {
"detected": true,
"version": "4.0.85",
"result": "malicious (high confidence)",
"update": "20230413"
},
"MicroWorld-eScan": {
"detected": true,
"version": "14.0.409.0",
"result": "Trojan.Ransom.Cerber.1",
"update": "20230417"
}
}
}
The JSON file contains Anti-Virus scan results organized by products. It is required to retrieve results related to the product "Elastic" only in csv format for further processing as below:
detected, version, result, update
true, "4.0.85", "malicious (high confidence)", "20230413"
Base on my research, able to extract the result by jq command as shown below:
jq-win64.exe ".scans.Elastic" scanresults.json
{
"detected": true,
"version": "4.0.85",
"result": "malicious (high confidence)",
"update": "20230413"
}
Tried but failed to use map function to extract the needed results in csv format. The reference link used is https://earthly.dev/blog/convert-to-from-json/.
$ cat simple.json| jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
"color","id","value"
"red",1,"#f00"
"green",2,"#0f0"
"blue",3,"#00f"
Thanks for provide solution and comments.