@@ -22,7 +22,6 @@ package main
2222
2323import (
2424 "encoding/csv"
25- "encoding/json"
2625 "flag"
2726 "fmt"
2827 "io"
@@ -137,6 +136,7 @@ Convert data1.csv to JSON blobs, one line per blob
137136 trimLeadingSpace bool
138137 fieldsPerRecord int
139138 reuseRecord bool
139+ pretty bool
140140)
141141
142142func fmtTxt (src string , appName string , version string ) string {
@@ -167,6 +167,7 @@ func main() {
167167 flag .BoolVar (& trimLeadingSpace , "trim-leading-space" , false , "trim leading space in fields for CSV input" )
168168 flag .BoolVar (& reuseRecord , "reuse-record" , false , "reuse the backing array" )
169169 flag .IntVar (& fieldsPerRecord , "fields-per-record" , 0 , "Set the number of fields expected in the CSV read, -1 to turn off" )
170+ flag .BoolVar (& pretty , "pretty" , false , "pretty print the JSON output" )
170171
171172 // Parse environment and options
172173 flag .Parse ()
@@ -260,7 +261,12 @@ func main() {
260261 object [fmt .Sprintf ("col_%d" , col )] = val
261262 }
262263 }
263- src , err := json .Marshal (object )
264+ var src []byte
265+ if pretty {
266+ src , err = datatools .JSONMarshalIndent (object , "" , " " )
267+ } else {
268+ src , err = datatools .JSONMarshal (object )
269+ }
264270 if err != nil {
265271 if ! quiet {
266272 fmt .Fprintf (eout , "error row %d, %s\n " , rowNo , err )
0 commit comments