Skip to main content
jq functions are filters, parameter is unnecessary
Source Link
Jonathan Allard
  • 19.5k
  • 11
  • 57
  • 76

The following filter is slightly different in that it will ensure every value is converted to a string. (Note: use jqjq 1.5+)

# For an array of many objects jq -f filter.jq (file) # For many objects (not within array) jq -s -f filter.jq (file) 
# For an array of many objects jq -f filter.jq [file] # For many objects (not within array) jq -s -f filter.jq [file] 

Filter: filter.jq

def tocsv($x): $x |(map(keys) |add |unique |sort ) as $cols |map(. as $row |$cols |map($row[.]|tostring) ) as $rows |$cols,$rows[] | @csv; tocsv(.) 

The following filter is slightly different in that it will ensure every value is converted to a string. (Note: use jq 1.5+)

# For an array of many objects jq -f filter.jq (file) # For many objects (not within array) jq -s -f filter.jq (file) 

Filter: filter.jq

def tocsv($x): $x |(map(keys) |add |unique |sort ) as $cols |map(. as $row |$cols |map($row[.]|tostring) ) as $rows |$cols,$rows[] | @csv; tocsv(.) 

The following filter is slightly different in that it will ensure every value is converted to a string. (jq 1.5+)

# For an array of many objects jq -f filter.jq [file] # For many objects (not within array) jq -s -f filter.jq [file] 

Filter: filter.jq

def tocsv: (map(keys) |add |unique |sort ) as $cols |map(. as $row |$cols |map($row[.]|tostring) ) as $rows |$cols,$rows[] | @csv; tocsv 
Source Link
TJR
  • 3.8k
  • 8
  • 40
  • 42

The following filter is slightly different in that it will ensure every value is converted to a string. (Note: use jq 1.5+)

# For an array of many objects jq -f filter.jq (file) # For many objects (not within array) jq -s -f filter.jq (file) 

Filter: filter.jq

def tocsv($x): $x |(map(keys) |add |unique |sort ) as $cols |map(. as $row |$cols |map($row[.]|tostring) ) as $rows |$cols,$rows[] | @csv; tocsv(.)