Skip to content

Commit be9d43e

Browse files
author
R. S. Doiel
committed
Merge branch 'main' of github.com:caltechlibrary/datatools into gh-pages
2 parents 5ac6d25 + 24eb061 commit be9d43e

140 files changed

Lines changed: 624 additions & 150 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CITATION.cff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ maintainers:
1515
orcid: ""
1616

1717
repository-code: "https://github.com/caltechlibrary/datatools"
18-
version: 1.3.0
18+
version: 1.3.2
1919
license-url: "https://data.caltech.edu/license"
2020
keywords: [ "csv", "excel", "sql", "json", "yaml", "xlsx", "golang", "bash" ]
21-
date-released: 2025-01-31
21+
date-released: 2025-05-13

INSTALL.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,37 @@ <h2 id="compiled-version">Compiled version</h2>
5959
</blockquote>
6060
<table>
6161
<thead>
62-
<tr class="header">
62+
<tr>
6363
<th>Platform</th>
6464
<th>Zip Filename</th>
6565
</tr>
6666
</thead>
6767
<tbody>
68-
<tr class="odd">
68+
<tr>
6969
<td>Windows</td>
7070
<td>datatools-VERSION_NUMBER-Windows-x86_64.zip</td>
7171
</tr>
72-
<tr class="even">
72+
<tr>
7373
<td>Windows</td>
7474
<td>datatools-VERSION_NUMBER-Windows-arm64.zip</td>
7575
</tr>
76-
<tr class="odd">
76+
<tr>
7777
<td>Mac OS X</td>
7878
<td>datatools-VERSION_NUMBER-macOS-x86_64.zip</td>
7979
</tr>
80-
<tr class="even">
80+
<tr>
8181
<td>Mac OS X</td>
8282
<td>datatools-VERSION_NUMBER-macOS-arm64.zip</td>
8383
</tr>
84-
<tr class="odd">
84+
<tr>
8585
<td>Linux/Intel</td>
8686
<td>datatools-VERSION_NUMBER-Linux-x86_64.zip</td>
8787
</tr>
88-
<tr class="even">
88+
<tr>
8989
<td>Linux/ARM 64</td>
9090
<td>datatools-VERSION_NUMBER-Linux-aarch64.zip</td>
9191
</tr>
92-
<tr class="odd">
92+
<tr>
9393
<td>Raspbery Pi OS</td>
9494
<td>datatools-VERSION_NUMBER-RaspberryPiOS-arm7.zip</td>
9595
</tr>

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ RELEASE_DATE=$(shell date +'%Y-%m-%d')
99

1010
RELEASE_HASH=$(shell git log --pretty=format:'%h' -n 1)
1111

12-
PROGRAMS = codemeta2cff csv2json csv2mdtable csv2tab csv2xlsx csvcleaner csvcols csvfind csvjoin csvrows finddir findfile json2toml json2yaml jsoncols jsonjoin jsonmunge jsonrange mergepath range reldate reltime sql2csv string tab2csv timefmt toml2json urlparse xlsx2csv xlsx2json yaml2json jsonobjects2csv
12+
PROGRAMS = codemeta2cff csv2json csv2jsonl csv2mdtable csv2tab csv2xlsx csvcleaner csvcols csvfind csvjoin csvrows finddir findfile json2toml json2yaml jsoncols jsonjoin jsonmunge jsonrange jsonobjects2csv mergepath range reldate reltime sql2csv string tab2csv timefmt toml2json urlparse xlsx2csv xlsx2json yaml2json
1313

14-
MAN_PAGES = codemeta2cff.1 csv2json.1 csv2mdtable.1 csv2tab.1 csv2xlsx.1 csvcleaner.1 csvcols.1 csvfind.1 csvjoin.1 csvrows.1 finddir.1 findfile.1 json2toml.1 json2yaml.1 jsoncols.1 jsonjoin.1 jsonmunge.1 jsonrange.1 mergepath.1 range.1 reldate.1 reltime.1 sql2csv.1 string.1 tab2csv.1 timefmt.1 toml2json.1 urlparse.1 xlsx2csv.1 xlsx2json.1 yaml2json.1 jsonobjects2csv.1
14+
MAN_PAGES = codemeta2cff.1 csv2json.1 csv2jsonl.1 csv2mdtable.1 csv2tab.1 csv2xlsx.1 csvcleaner.1 csvcols.1 csvfind.1 csvjoin.1 csvrows.1 finddir.1 findfile.1 json2toml.1 json2yaml.1 jsoncols.1 jsonjoin.1 jsonmunge.1 jsonrange.1 jsonobjects2csv.1 mergepath.1 range.1 reldate.1 reltime.1 sql2csv.1 string.1 tab2csv.1 timefmt.1 toml2json.1 urlparse.1 xlsx2csv.1 xlsx2json.1 yaml2json.1
1515

1616
PACKAGE = $(shell ls -1 *.go)
1717

@@ -214,6 +214,7 @@ gen_batfiles: .FORCE
214214
snap: dist/datatools_$(VERSION)_amd64.snap
215215

216216
release: .FORCE clean build man gen_batfiles distribute_docs dist/Linux-armv7l dist/Linux-aarch64 dist/Linux-x86_64 dist/macOS-x86_64 dist/macOS-arm64 dist/Windows-x86_64 dist/Windows-arm64
217+
echo "ready to run ./release.bash"
217218

218219

219220
.FORCE:

about.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
<section>
2727
<h1 id="about-this-software">About this software</h1>
28-
<h2 id="datatools-1.3.0">datatools 1.3.0</h2>
28+
<h2 id="datatools-1.3.2">datatools 1.3.2</h2>
2929
<h3 id="authors">Authors</h3>
3030
<ul>
3131
<li>R. S. Doiel</li>

about.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ authors:
1111
orcid: ""
1212

1313
repository-code: "https://github.com/caltechlibrary/datatools"
14-
version: 1.3.0
14+
version: 1.3.2
1515
license-url: "https://data.caltech.edu/license"
1616
keywords: [ "csv", "excel", "sql", "json", "yaml", "xlsx", "golang", "bash" ]
17-
date-released: 2025-01-31
17+
date-released: 2025-05-13
1818
---
1919

2020
About this software
2121
===================
2222

23-
## datatools 1.3.0
23+
## datatools 1.3.2
2424

2525
### Authors
2626

cmd/csv2jsonl/csv2jsonl.go

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
// csv2json - is a command line that takes CSV input from stdin and
2+
// writes out JSON-L expression. It includes support for using the first
3+
// row as field names or default fieldnames (e.g. col0, col1, col2).
4+
// Additionally it can output the resulting JSON data structures as a
5+
// JSON array or individual JSON blobs (one line per blob).
6+
//
7+
// @author R. S. Doiel, <rsdoiel@caltech.edu>
8+
//
9+
// Copyright (c) 2021, Caltech
10+
// All rights not granted herein are expressly reserved by Caltech.
11+
//
12+
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
13+
//
14+
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
15+
//
16+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
17+
//
18+
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21+
package main
22+
23+
import (
24+
"encoding/csv"
25+
"flag"
26+
"fmt"
27+
"io"
28+
"os"
29+
"path"
30+
"strings"
31+
32+
// My packages
33+
"github.com/caltechlibrary/datatools"
34+
)
35+
36+
var (
37+
helpText = `%{app_name}(1) user manual | version {version} {release_hash}
38+
% R. S. Doiel
39+
% {release_date}
40+
41+
# NAME
42+
43+
{app_name}
44+
45+
# SYNOPSIS
46+
47+
{app_name} [OPTIIONS]
48+
49+
# DESCRIPTION
50+
51+
csv2jsonl reads CSV from stdin and writes a JSON-L to stdout. JSON output
52+
is one object per line. See https://jsonlines.org.
53+
54+
# OPTIONS
55+
56+
-help
57+
: display help
58+
59+
-license
60+
: display license
61+
62+
-version
63+
: display version
64+
65+
-d, -delimiter
66+
: set the delimter character
67+
68+
-examples
69+
: display example(s)
70+
71+
-fields-per-record
72+
: Set the number of fields expected in the CSV read, -1 to turn off
73+
74+
-i, -input
75+
: input filename
76+
77+
-nl, -newline
78+
: include trailing newline in output
79+
80+
-o, -output
81+
: output filename
82+
83+
-quiet
84+
: suppress error output
85+
86+
-reuse-record
87+
: reuse the backing array
88+
89+
-trim-leading-space
90+
: trim leading space in fields for CSV input
91+
92+
-use-header
93+
: treat the first row as field names
94+
95+
-use-lazy-quotes
96+
: use lazy quotes for for CSV input
97+
98+
99+
# EXAMPLES
100+
101+
Convert data1.csv to data1.jsonl using Unix pipes.
102+
103+
~~~
104+
cat data1.csv | csv2jsonl > data1.jsonl
105+
~~~
106+
107+
Convert data1.csv to JSON line (one object line per blob)
108+
109+
~~~
110+
csv2jsonl data1.csv
111+
~~~
112+
113+
`
114+
115+
// Standard Options
116+
showHelp bool
117+
showLicense bool
118+
showVersion bool
119+
inputFName string
120+
outputFName string
121+
quiet bool
122+
newLine bool
123+
eol string
124+
125+
// Application Options
126+
useHeader bool
127+
asBlobs bool
128+
delimiter string
129+
lazyQuotes bool
130+
trimLeadingSpace bool
131+
fieldsPerRecord int
132+
reuseRecord bool
133+
)
134+
135+
func main() {
136+
appName := path.Base(os.Args[0])
137+
version := datatools.Version
138+
license := datatools.LicenseText
139+
releaseDate := datatools.ReleaseDate
140+
releaseHash := datatools.ReleaseHash
141+
142+
// Standard Options
143+
flag.BoolVar(&showHelp, "help", showHelp, "display help")
144+
flag.BoolVar(&showLicense, "license", showLicense, "display license")
145+
flag.BoolVar(&showVersion, "version", showVersion, "display version")
146+
flag.StringVar(&inputFName, "i", "", "input filename")
147+
flag.StringVar(&inputFName, "input", "", "input filename")
148+
flag.StringVar(&outputFName, "o", "", "output filename")
149+
flag.StringVar(&outputFName, "output", "", "output filename")
150+
flag.BoolVar(&quiet, "quiet", false, "suppress error output")
151+
flag.BoolVar(&newLine, "nl", true, "include trailing newline in output")
152+
flag.BoolVar(&newLine, "newline", true, "include trailing newline in output")
153+
154+
// App Options
155+
flag.BoolVar(&useHeader, "use-header", true, "treat the first row as field names")
156+
flag.StringVar(&delimiter, "d", "", "set the delimter character")
157+
flag.StringVar(&delimiter, "delimiter", "", "set the delimter character")
158+
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for for CSV input")
159+
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in fields for CSV input")
160+
flag.BoolVar(&reuseRecord, "reuse-record", false, "reuse the backing array")
161+
flag.IntVar(&fieldsPerRecord, "fields-per-record", 0, "Set the number of fields expected in the CSV read, -1 to turn off")
162+
163+
// Parse environment and options
164+
flag.Parse()
165+
166+
// Setup IO
167+
var err error
168+
169+
in := os.Stdin
170+
out := os.Stdout
171+
eout := os.Stderr
172+
173+
if inputFName != "" && inputFName != "-" {
174+
in, err = os.Open(inputFName)
175+
if err != nil {
176+
fmt.Fprintln(eout, err)
177+
os.Exit(1)
178+
}
179+
defer in.Close()
180+
}
181+
if outputFName != "" && outputFName != "-" {
182+
out, err = os.Create(outputFName)
183+
if err != nil {
184+
fmt.Fprintln(eout, err)
185+
os.Exit(1)
186+
}
187+
defer out.Close()
188+
}
189+
190+
// Process options
191+
if showHelp {
192+
fmt.Fprintf(out, "%s\n", datatools.FmtHelp(helpText, appName, version, releaseDate, releaseHash))
193+
os.Exit(0)
194+
}
195+
if showLicense == true {
196+
fmt.Fprintf(out, "%s\n", license)
197+
os.Exit(0)
198+
}
199+
if showVersion == true {
200+
fmt.Fprintf(out, "datatools, %s %s %s\n", appName, version, releaseHash)
201+
os.Exit(0)
202+
}
203+
if newLine {
204+
eol = "\n"
205+
}
206+
207+
rowNo := 0
208+
fieldNames := []string{}
209+
r := csv.NewReader(in)
210+
r.Comment = '#'
211+
r.FieldsPerRecord = fieldsPerRecord
212+
r.LazyQuotes = lazyQuotes
213+
r.TrimLeadingSpace = trimLeadingSpace
214+
r.ReuseRecord = reuseRecord
215+
if delimiter != "" {
216+
r.Comma = datatools.NormalizeDelimiterRune(delimiter)
217+
}
218+
if useHeader == true {
219+
row, err := r.Read()
220+
if err == io.EOF {
221+
fmt.Fprintln(eout, "No data")
222+
os.Exit(1)
223+
}
224+
if err != nil {
225+
fmt.Fprintln(eout, err)
226+
os.Exit(1)
227+
}
228+
for _, val := range row {
229+
fieldNames = append(fieldNames, strings.TrimSpace(val))
230+
}
231+
rowNo++
232+
}
233+
hasError := false
234+
object := map[string]interface{}{}
235+
for {
236+
row, err := r.Read()
237+
if err == io.EOF {
238+
break
239+
}
240+
if err != nil {
241+
fmt.Fprintln(eout, err)
242+
os.Exit(1)
243+
}
244+
245+
// Pad the fieldnames if necessary
246+
object = map[string]interface{}{}
247+
for col, val := range row {
248+
if col < len(fieldNames) {
249+
object[fieldNames[col]] = val
250+
} else {
251+
object[fmt.Sprintf("col_%d", col)] = val
252+
}
253+
}
254+
var src []byte
255+
src, err = datatools.JSONMarshal(object)
256+
if err != nil {
257+
if !quiet {
258+
fmt.Fprintf(eout, "error row %d, %s\n", rowNo, err)
259+
}
260+
hasError = true
261+
}
262+
fmt.Fprintf(out, "%s%s", src, eol)
263+
}
264+
if hasError == true {
265+
os.Exit(1)
266+
}
267+
}

codemeta.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
}
3030
],
3131
"dateCreated": "2017-02-06",
32-
"dateModified": "2025-01-31",
33-
"datePublished": "2025-01-31",
32+
"dateModified": "2025-05-13",
33+
"datePublished": "2025-05-13",
3434
"description": "A set of command line tools for working with CSV, Excel Workbooks, JSON and structured text documents.",
3535
"funder": {
3636
"@id": "https://ror.org/5dxps055",
@@ -61,9 +61,9 @@
6161
"Golang >= 1.23.5",
6262
"Pandoc >= 3.1"
6363
],
64-
"version": "1.3.0",
64+
"version": "1.3.2",
6565
"developmentStatus": "active",
6666
"issueTracker": "https://github.com/caltechlibrary/datatools/issues",
6767
"downloadUrl": "https://github.com/caltechlibrary/datatools/releases/",
68-
"releaseNotes": "Added CRLF option on CSV write to better support Windows, see issue #24"
68+
"releaseNotes": "Added cli, csv2jsonl, that will convert a CSV document into a JSON lines list of objects."
6969
}

0 commit comments

Comments
 (0)