使用正确的引号从 Bigquery 中导出 JSON 中 table 的值

Export JSON value in table from Bigquery with correct quotes

我想将 Bigquery 中的列值导出为:

| NAME    | JSON          |
| abc     | {"test": 1}   |

但是,当我想通过 python 代码将其导出到压缩 csv/tsv 到 google 云存储时,字段分隔符 = '\t' (https://google-cloud.readthedocs.io/en/latest/bigquery/generated/google.cloud.bigquery.client.Client.extract_table.html) ,我总是得到类似的东西:

| NAME    | JSON            | 
| abc     | "{""test"": 1}" |

我知道转义,我一直在尝试很多转义的可能性(使用 "" 转义 " 或添加 -values),但我似乎无法将导出为:

{"test": 1}

请帮帮我?

工具输出正确,但您需要阅读 CSV 文件的标准 RFC 4180 才能了解原因。

基本上,JSON spectest 需要有双引号,即 "test".

CSV 中允许在整个字段周围使用双引号。但 CSV 规范还指出,在带有引号字段的 CSV 中,内部引号是重复的。这是 RFC 4180 第 2 节的规则 7:

  1. If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

    "aaa","b""bb","ccc"

那么解决方案是什么?

可能,您需要符合 RFC 4180 标准的 CSV reader,因此您不会在使用文件的地方自己编写解析代码。

您可以将双引号替换为单双引号,大括号中的引号替换为如下所示:

sed -e 's/"{/{/g; s/}"/}/g; s/""/"/g;' in.csv > out.csv

转型

"{""test"": 1}"

{ "test": 1}

或在 JavaScript 中使用 String.replace,但生成的 csv 文件不符合 RFC 4180。