@@ -95,6 +95,9 @@ is one object per line. See https://jsonlines.org.
9595-use-lazy-quotes 
9696: use lazy quotes for for CSV input 
9797
98+ -for-dataset COLUMN_NO 
99+ : if COLUMN_NO is greater than -1 then, generate a dataset load compatible version of the CSV file 
100+ using COLUMN_NO as key. 
98101
99102# EXAMPLES 
100103
@@ -130,6 +133,7 @@ Convert data1.csv to JSON line (one object line per blob)
130133	trimLeadingSpace  bool 
131134	fieldsPerRecord   int 
132135	reuseRecord       bool 
136+ 	forDataset        int 
133137)
134138
135139func  main () {
@@ -159,6 +163,7 @@ func main() {
159163	flag .BoolVar (& trimLeadingSpace , "trim-leading-space" , false , "trim leading space in fields for CSV input" )
160164	flag .BoolVar (& reuseRecord , "reuse-record" , false , "reuse the backing array" )
161165	flag .IntVar (& fieldsPerRecord , "fields-per-record" , 0 , "Set the number of fields expected in the CSV read, -1 to turn off" )
166+ 	flag .IntVar (& forDataset , "for-dataset" , - 1 , "generate a dataset compatible JSON lines output using column number as key" )
162167
163168	// Parse environment and options 
164169	flag .Parse ()
@@ -244,12 +249,16 @@ func main() {
244249
245250		// Pad the fieldnames if necessary 
246251		object  =  map [string ]interface {}{}
252+ 		key  :=  "" 
247253		for  col , val  :=  range  row  {
248254			if  col  <  len (fieldNames ) {
249255				object [fieldNames [col ]] =  val 
250256			} else  {
251257				object [fmt .Sprintf ("col_%d" , col )] =  val 
252258			}
259+ 			if  (col  ==  forDataset ) {
260+ 				key  =  fmt .Sprintf ("%s" , val );
261+ 			}
253262		}
254263		var  src  []byte 
255264		src , err  =  datatools .JSONMarshal (object )
@@ -259,7 +268,17 @@ func main() {
259268			}
260269			hasError  =  true 
261270		}
262- 		fmt .Fprintf (out , "%s%s" , src , eol )
271+ 		if  (forDataset  >=  0 ) {
272+ 			if  (key  ==  "" ) {
273+ 				if  ! quiet  {
274+ 					fmt .Fprintf (eout , "error row, mising key value for column %d, row %d\n " , forDataset , rowNo )
275+ 				}
276+ 			}
277+ 			fmt .Fprintf (out , `{%q:%q,%q:%s}%s` , "key" , key , "object" , src , eol )
278+ 		} else  {
279+ 			fmt .Fprintf (out , "%s%s" , src , eol )
280+ 		}
281+ 		rowNo ++ 
263282	}
264283	if  hasError  ==  true  {
265284		os .Exit (1 )
0 commit comments