Arun
Arun

Reputation: 1220

Parsing Complex JSON file with JQ Issue

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

virus.json

{
  "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"
}

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.

I tried ,

1

$ 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

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

Output

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

Any suggestion on how to get the results ?

Upvotes: 3

Views: 266

Answers (3)

peak
peak

Reputation: 116870

@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)"'

Upvotes: 3

P....
P....

Reputation: 18391

jq -r ".detected,.result" dum_1.json |awk -v OFS=, 'NR%2{x=$0;next} {print x OFS $0}'
true,Trojan.Win32.Generic!BT
true,FileCryptor.NJX
true,Generic.Ransom.Purge.DC87C66E

Upvotes: 1

oliv
oliv

Reputation: 13259

You could use sed:

jq -r ".detected,.result" dum_1.json | sed 'N;s/\n/ , /'
true , Trojan.Win32.Generic!BT
true , FileCryptor.NJX
true , Generic.Ransom.Purge.DC87C66E

The sed N command read the next line, joining the 2 lines together.

The s command is replacing the newline \n with the wanted pattern ,.

Upvotes: 1

Related Questions