Skip to content

Commit 4818152

Browse files
committed
csv2json: fix values with double quotes and new line symbols. #291
1 parent 6e4f5c5 commit 4818152

File tree

6 files changed

+110
-34
lines changed

6 files changed

+110
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- add flag `--nr-width`.
1212
- `csvtk replace`:
1313
- fix implementing `{nr}`. [#286](https://github.com/shenwei356/csvtk/issues/286)
14+
- `csvtk csv2json`:
15+
- fix values with double quotes and new line symbols. [#291](https://github.com/shenwei356/csvtk/issues/291)
1416
- `csvtk split`:
1517
- support customize output file prefix and subdirectory from prefix of keys. [#288](https://github.com/shenwei356/csvtk/issues/288)
1618
- `csvtk spread`:

csvtk/cmd/box.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ Notes:
178178

179179
b.GlyphStyle.Color = plotutil.Color(j)
180180
b.GlyphStyle.Radius = pointSize
181-
182181
j++
183182
}
184183

csvtk/cmd/csv2json.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package cmd
2222

2323
import (
2424
"fmt"
25+
"regexp"
2526
"runtime"
2627
"strconv"
2728
"strings"
@@ -262,12 +263,18 @@ func processJSONValue(val string, blanks bool, parseNum bool) string {
262263
return "false"
263264
case "", "na", "n/a", "none", "null", ".":
264265
if blanks {
265-
return `""`
266+
return `"` + val + `"`
266267
}
267268
return "null"
268269
}
269270
if parseNum && reDigitals.MatchString(val) {
270271
return val
271272
}
273+
274+
val = reEscaple.ReplaceAllString(val, `\$1`)
275+
val = strings.ReplaceAll(val, "\n", "\\n")
276+
272277
return `"` + val + `"`
273278
}
279+
280+
var reEscaple = regexp.MustCompile(`(["\\])`)

doc/docs/download.md

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,52 @@
66

77
## Current Version
88

9-
- [csvtk v0.30.0](https://github.com/shenwei356/csvtk/releases/tag/v0.30.0)
10-
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.30.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.30.0)
11-
- `csvtk`:
12-
- grouping subcommands in help message.
13-
- add a new global flag `--quiet`. [#261](https://github.com/shenwei356/csvtk/issues/261)
14-
- add a new global flag `-U, --delete-header` for disable outputing the header row. Supported commands: concat, csv2tab/tab2csv, csv2xlsx/xlsx2csv, cut, filter, filter2, freq, fold/unfold, gather, fmtdate, grep, head, join, mutate, mutate2, replace, round, sample. [#258](https://github.com/shenwei356/csvtk/issues/258)
15-
- support more commands with `-Z/--show-row-number`: head.
16-
- `csvtk dim`:
17-
- fix duplicated rows for multiple input files, this bug was introduced in v0.27.0.
18-
- `csvtk concat`:
19-
- fix panic when no data found. [#259](https://github.com/shenwei356/csvtk/issues/259)
20-
- `csvtk spread`:
21-
- fix flag checking of `-k` and `-v`.
22-
- `csvtk sort`:
23-
- fix ordering when given multiple custom levels.
24-
- `csvtk filter/filter2`:
25-
- fix printing row number with `-Z`.
26-
- `csvtk xls2csv`:
27-
- output raw data. [#262](https://github.com/shenwei356/csvtk/issues/262)
28-
- `csvtk pretty`:
29-
- change the default value of `-n/--buf-rows` from 128 to 1024, and 0 for loading all data.
30-
- `csvtk join`:
31-
- new flag `-s/--suffix` for adding suffixes to colnames from each file. [#263](https://github.com/shenwei356/csvtk/issues/263)
9+
- [csvtk v0.31.0](https://github.com/shenwei356/csvtk/releases/tag/v0.31.0)
10+
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.31.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.31.0)
11+
- `csvtk sort/join`:
12+
- faster speed and lower memory.
13+
- `csvtk sort`:
14+
- do not panic for empty input. [#287](https://github.com/shenwei356/csvtk/issues/287)
15+
- `csvtk summary`:
16+
- fix the order of columns. [#282](https://github.com/shenwei356/csvtk/issues/282)
17+
- `csvtk rename2`:
18+
- fix `-n/--start-num`. [#286](https://github.com/shenwei356/csvtk/issues/286)
19+
- add flag `--nr-width`.
20+
- `csvtk replace`:
21+
- fix implementing `{nr}`. [#286](https://github.com/shenwei356/csvtk/issues/286)
22+
- `csvtk csv2json`:
23+
- fix values with double quotes and new line symbols.
24+
- `csvtk split`:
25+
- support customize output file prefix and subdirectory from prefix of keys. [#288](https://github.com/shenwei356/csvtk/issues/288)
26+
- `csvtk spread`:
27+
- add a new alias "scatter" to "spread". [#265](https://github.com/shenwei356/csvtk/issues/265)
28+
- `csvtk grep`:
29+
- do not show progress.
30+
- `csvtk fix-quotes`:
31+
- new flag `-b, --buffer-size`.
32+
- `csvtk plot`:
33+
- new flag `--scale` for scaling the image width/height, tick, axes, line/point and font sizes proportionally, adviced by @tseemann.
34+
- `csvtk plot line`:
35+
- only add legend for more than one group. [#279](https://github.com/shenwei356/csvtk/issues/279)
36+
- sort points by X for plotting lines. [#280](https://github.com/shenwei356/csvtk/issues/280)
37+
- `csvtk hist`:
38+
- new flags: `--line-width`.
39+
- `csvtk box`:
40+
- plots of different groups have different colors now.
41+
- new flags: `--line-width`, `--point-size`, and `color-index`.
42+
3243

3344
### Links
3445

3546
OS |Arch |File, 中国镜像 |Download Count
3647
:------|:---------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
37-
Linux |32-bit |[csvtk_linux_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_linux_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_linux_386.tar.gz)
38-
Linux |**64-bit**|[**csvtk_linux_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_linux_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_linux_amd64.tar.gz)
39-
Linux |**64-bit**|[**csvtk_linux_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_linux_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_linux_arm64.tar.gz)
40-
macOS |**64-bit**|[**csvtk_darwin_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_darwin_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_darwin_amd64.tar.gz)
41-
macOS |**arm64** |[**csvtk_darwin_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_darwin_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_darwin_arm64.tar.gz)
42-
Windows|32-bit |[csvtk_windows_386.exe.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_windows_386.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_386.exe.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_windows_386.exe.tar.gz)
43-
Windows|**64-bit**|[**csvtk_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_windows_amd64.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_amd64.exe.tar.gz)|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.30.0/csvtk_windows_amd64.exe.tar.gz)
48+
Linux |32-bit |[csvtk_linux_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_linux_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_linux_386.tar.gz)
49+
Linux |**64-bit**|[**csvtk_linux_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_linux_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_linux_amd64.tar.gz)
50+
Linux |**64-bit**|[**csvtk_linux_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_linux_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_linux_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_linux_arm64.tar.gz)
51+
macOS |**64-bit**|[**csvtk_darwin_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_darwin_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_darwin_amd64.tar.gz)
52+
macOS |**arm64** |[**csvtk_darwin_arm64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_darwin_arm64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_darwin_arm64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_arm64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_darwin_arm64.tar.gz)
53+
Windows|32-bit |[csvtk_windows_386.exe.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_windows_386.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_386.exe.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_windows_386.exe.tar.gz)
54+
Windows|**64-bit**|[**csvtk_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_windows_amd64.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/csvtk/csvtk_windows_amd64.exe.tar.gz)|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.31.0/csvtk_windows_amd64.exe.tar.gz)
4455

4556
**Notes**
4657

@@ -155,6 +166,29 @@ fish:
155166

156167
## Release history
157168

169+
- [csvtk v0.30.0](https://github.com/shenwei356/csvtk/releases/tag/v0.30.0)
170+
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.30.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.30.0)
171+
- `csvtk`:
172+
- grouping subcommands in help message.
173+
- add a new global flag `--quiet`. [#261](https://github.com/shenwei356/csvtk/issues/261)
174+
- add a new global flag `-U, --delete-header` for disable outputing the header row. Supported commands: concat, csv2tab/tab2csv, csv2xlsx/xlsx2csv, cut, filter, filter2, freq, fold/unfold, gather, fmtdate, grep, head, join, mutate, mutate2, replace, round, sample. [#258](https://github.com/shenwei356/csvtk/issues/258)
175+
- support more commands with `-Z/--show-row-number`: head.
176+
- `csvtk dim`:
177+
- fix duplicated rows for multiple input files, this bug was introduced in v0.27.0.
178+
- `csvtk concat`:
179+
- fix panic when no data found. [#259](https://github.com/shenwei356/csvtk/issues/259)
180+
- `csvtk spread`:
181+
- fix flag checking of `-k` and `-v`.
182+
- `csvtk sort`:
183+
- fix ordering when given multiple custom levels.
184+
- `csvtk filter/filter2`:
185+
- fix printing row number with `-Z`.
186+
- `csvtk xls2csv`:
187+
- output raw data. [#262](https://github.com/shenwei356/csvtk/issues/262)
188+
- `csvtk pretty`:
189+
- change the default value of `-n/--buf-rows` from 128 to 1024, and 0 for loading all data.
190+
- `csvtk join`:
191+
- new flag `-s/--suffix` for adding suffixes to colnames from each file. [#263](https://github.com/shenwei356/csvtk/issues/263)
158192
- [csvtk v0.29.0](https://github.com/shenwei356/csvtk/releases/tag/v0.29.0)
159193
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.29.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.29.0)
160194
- new commands:

doc/docs/usage.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Usage
124124
```text
125125
csvtk -- a cross-platform, efficient and practical CSV/TSV toolkit
126126
127-
Version: 0.30.0
127+
Version: 0.31.0
128128
129129
Author: Wei Shen <[email protected]>
130130
@@ -1088,7 +1088,34 @@ Examples
10881088
"ID": "1e-3",
10891089
"room": "2",
10901090
"name": "",
1091-
"status": ""
1091+
"status": "N/A"
1092+
}
1093+
]
1094+
1095+
- values with `"`, `\`, `\n`.
1096+
1097+
$ cat testdata/data4json2.csv
1098+
test
1099+
none
1100+
"Make America ""great"" again"
1101+
\nations
1102+
"This is a
1103+
MULTILINE
1104+
string"
1105+
1106+
$ csvtk csv2json testdata/data4json2.csv
1107+
[
1108+
{
1109+
"test": null
1110+
},
1111+
{
1112+
"test": "Make America \"great\" again"
1113+
},
1114+
{
1115+
"test": "\\nations"
1116+
},
1117+
{
1118+
"test": "This is a\nMULTILINE\nstring"
10921119
}
10931120
]
10941121

testdata/data4json2.csv

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test
2+
none
3+
"Make America ""great"" again"
4+
\nations
5+
"This is a
6+
MULTILINE
7+
string"

0 commit comments

Comments
 (0)