【发布时间】:2020-05-03 19:56:59
【问题描述】:
我正在使用jq 将嵌套的.json 文件转换为.csv,但是,当我有一个特定的键、值 在 JSON 中。我使用的脚本来自this answer,如果我从输入文件中删除一行,它就可以工作。
错误:
jq: error (at example.json:0): object ({"favicon":...) is not valid in a csv row
错误消息的描述性不是很好,因为它提到的对象似乎不是罪魁祸首。
json2csv.jq
paths as $path
| {path: $path, value: getpath($path)}
| select(.value|type == "object" )
| select( [.value[]][0] | type != "object")
| .path + ([.value[]])
| @csv
example.json(使用 https://jsonlint.com 验证):
{
"items": [
{
"id": 1234,
"title": "Title of Item",
"internet": {
"favicon": "https://example.com/icon.png",
"domain": "https://example.com"
},
"image": {
"gif": null,
"main": "https://example.com/image_large.png"
},
"description": {
"origin": null,
"resource": null
},
"referral": {
"owner": null,
"name": null
},
"url": "https://example.com/image.png?version=1"
}
],
"other": {
"data": 0,
"notes": 1,
"total": 1,
"misc": 1
}
}
从 JSON 中删除以下内容不会从 jq 引发错误:
, "url": "https://example.com/image.png?version=1"
调用:
$ jq -er -f json2csv.jq example.json
我无法真正更改.json 文件,因为它需要有问题的行,所以我正在寻找jq 的解决方案。
【问题讨论】:
-
请遵循minimal reproducible example 指南,描述和/或显示预期输出(最好同时显示)。
-
@peak:以上都是例子;输出并不重要,因为我可以根据需要进行调整。错误是问题所在,没有过去我没有输出。
-
@peak:成功了!如果您想将其发布为答案以及其他方式失败的原因,我很乐意接受,谢谢。