func File2InMemory(csvFilename string, hasHeaderLine bool) ([]byte, error) {
	csvFile, err := os.Open(csvFilename)
	if err != nil {
		return nil, errors.Wrapf(err, "could not open specified csv file, filename: %s", csvFilename)
	}
	defer csvFile.Close()

	r := csv.NewReader(csvFile)

	var (
		elements = make(map[string]map[string]string)
		counter  = 0
	)

	if hasHeaderLine {
		headers, err := r.Read()
		if err != nil {
			return nil, errors.Wrap(err, "encountered an error attempting to read the header line")
		}

		for line, err := r.Read(); err != io.EOF; line, err = r.Read() {
			innerMap := make(map[string]string)
			for i := 0; i < len(line); i++ {
				innerMap[headers[i]] = line[i]
			}
			elements[fmt.Sprintf("record_%d", counter)] = innerMap
			counter++
		}
	} else {
		for line, err := r.Read(); err != io.EOF; line, err = r.Read() {
			innerMap := make(map[string]string)
			for i := 0; i < len(line); i++ {
				innerMap[fmt.Sprintf("field_%d", i)] = line[i]
			}
			elements[fmt.Sprintf("record_%d", counter)] = innerMap
			counter++
		}
	}

	jsonData, err := json.MarshalIndent(elements, "", " ")
	if err != nil {
		return nil, errors.Wrap(err, "json.Marhsal encountered an error attempting to Marshal(elements) into []byte")
	}

	return jsonData, nil
}
// File2InMemory pulls data from a csv file and returns it as a json marshalled slice of bytes, along with an error.
// If the csv file contains an optional header line, set hasHeaderLine to true to use those header values to create a more descriptive json.
File2InMemory pulls data from a csv file and returns it as a json marshalled slice of bytes, along with an error.
If the csv file contains an optional header line, set hasHeaderLine to true to use those header values to create a more descriptive json.
// File2InMemory pulls data from a csv file and returns it as a json marshalled slice of bytes along with an error . // If the csv file contains an optional header line set hasHeaderLine to true to use those header values to create a more descriptive json .
func File2InMemory(csvFilename string, hasHeaderLine bool) ([]byte, error) {
	csvFile, err := os.Open(csvFilename)
	if err != nil {
		return nil, errors.Wrapf(err, "could not open specified csv file, filename: %s", csvFilename)
	}
	defer csvFile.Close()
	r := csv.NewReader(csvFile)
	var (
		elements = make(map[string]map[string]string)
		counter  = 0
	)
	if hasHeaderLine {
		headers, err := r.Read()
		if err != nil {
			return nil, errors.Wrap(err, "encountered an error attempting to read the header line")
		}
		for line, err := r.Read(); err != io.EOF; line, err = r.Read() {
			innerMap := make(map[string]string)
			for i := 0; i < len(line); i++ {
				innerMap[headers[i]] = line[i]
			}
			elements[fmt.Sprintf("record_%d", counter)] = innerMap
			counter++
		}
	} else {
		for line, err := r.Read(); err != io.EOF; line, err = r.Read() {
			innerMap := make(map[string]string)
			for i := 0; i < len(line); i++ {
				innerMap[fmt.Sprintf("field_%d", i)] = line[i]
			}
			elements[fmt.Sprintf("record_%d", counter)] = innerMap
			counter++
		}
	}
	jsonData, err := json.MarshalIndent(elements, "", " ")
	if err != nil {
		return nil, errors.Wrap(err, "json.Marhsal encountered an error attempting to Marshal(elements) into []byte")
	}
	return jsonData, nil
}
func File2InMemory ( csvFilename string , hasHeaderLine bool ) ( [ ] byte , error ) { csvFile , err := os . Open ( csvFilename ) if err != nil { return nil , errors . Wrapf ( err , " " , csvFilename ) } defer csvFile . Close ( ) r := csv . NewReader ( csvFile ) var ( elements = make ( map [ string ] map [ string ] string ) counter = 0 ) if hasHeaderLine { headers , err := r . Read ( ) if err != nil { return nil , errors . Wrap ( err , " " ) } for line , err := r . Read ( ) ; err != io . EOF ; line , err = r . Read ( ) { innerMap := make ( map [ string ] string ) for i := 0 ; i < len ( line ) ; i ++ { innerMap [ headers [ i ] ] = line [ i ] } elements [ fmt . Sprintf ( " " , counter ) ] = innerMap counter ++ } } else { for line , err := r . Read ( ) ; err != io . EOF ; line , err = r . Read ( ) { innerMap := make ( map [ string ] string ) for i := 0 ; i < len ( line ) ; i ++ { innerMap [ fmt . Sprintf ( " " , i ) ] = line [ i ] } elements [ fmt . Sprintf ( " " , counter ) ] = innerMap counter ++ } } jsonData , err := json . MarshalIndent ( elements , " " , " " ) if err != nil { return nil , errors . Wrap ( err , " " ) } return jsonData , nil }
File2InMemory pulls data from a csv file and returns it as a json marshalled slice of bytes, along with an error.
File2InMemory pulls data from a csv file and returns it as a json marshalled slice of bytes along with an error .