2

I was just introduced to JQ in a question I posted about an hour ago, I'm parsing a very large database however, with JQ the output looks like this:

"removedforprivacy@gmail.com"  
"john"  
"smith"  
null  
null  
"123 road st"  
null  
"Columbia"  
"29203"  
"SC"  
null  

instead I want it to look like this:

"removedforprivacy@gmail.com" "john" "smith" null null "123 road st" null "Columbia" "29203" "SC" null

or even better:

"removedforprivacy@gmail.com","john","smith",null,null,"123,road,st",null,"Columbia","29203","SC",null

I'm currently using this command:

jq -c '(.email, .first_name, .last_name, .ip, .address, .address1, .address2, .city, .zip, .state, .phone)' file.json > file2.json 

I've tried using this command as well:

jq -compact-output '(.email, .first_name, .last_name, .ip, .address, .address1, .address2, .city, .zip, .state, .phone)' file.json > file2.json 

but file2.json still shows data like this:

"removedforprivacy@gmail.com"  
"john"  
"smith"  
null  
null  
"123 road st"  
null  
"Columbia"  
"29203"  
"SC"  
null  

In short, I'd like to turn the output into something that looks like a csv or is a csv so I can manage it better.

The command isn't working and just need this command for a one time use.

jemand771
  • 457
  • 1
  • 6
  • 17
RayCrush
  • 29
  • 2
  • 12

1 Answers1

2

If you want CSV then you could of course simply use the @csv filter, but @csv converts null to an empty field:

"removedforprivacy@gmail.com","john","smith",,,"123 road st",,"Columbia","29203","SC",

To handle strings and null the way you seem to want, you could use @tsv as follows in conjunction with the -r command-line option:

map(if type == "string" then "\"\(.)\"" else "null" end)
| @tsv | gsub("\t";",") 

With your input, this produces:

"removedforprivacy@gmail.com","john","smith",null,null,"123 road st",null,"Columbia","29203","SC",null

For other variations, you may wish to use join/1 and/or the -j command-line option.

The -c command-line option

As the manual states:

Using this option will result in more compact output by instead putting each JSON object on a single line.

Here "JSON object" means "JSON entity".

peak
  • 105,803
  • 17
  • 152
  • 177
  • is it possible that you write the command out for me? i asked another question and i'm having issues, i used this command `jq -r 'to_entries[] | [.email, .first_name, .last_name, .ip, .address, .address1, .address2, .city, .zip, .state, .phone] | @csv' file.json > file3.json` but it's giving empty lines that look like this: ` ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ` – RayCrush Feb 16 '18 at 02:20
  • Why are you using `to_entries`? Based on the original question, it looks like you should start with something like `jq -r '[.email, .first_name, ...] | @csv'` – peak Feb 16 '18 at 02:27
  • oh my !!!! Thank you so much, i've been at this for hours, i have no idea why it didn't work earlier i used a similar command, thank you so much @peak !!!! <3 <3 <3 <3 – RayCrush Feb 16 '18 at 02:29