使用 jq bash 将 json 转换为 csv
convert json to csv using jq bash
有一个 JSON 数据如下
"metric": {
"name" : "name1"
},
"values": [
[
16590879,
"0.043984349"
],
"values": [
[
16590876,
"0.043983444"
]
]
}
}
写在 jq 下面,但没有给出正确的结果
jq -r '[.metric.name,(.values[] | map(.) | @csv)'
实际结果
[
"name1",
"16590879",\"0.043984349\"",
"16590876",\"0.043983444\"",
"16590874",\"0.043934345\""
预期结果
name1,16590879,0.043984349
name1,16590876,0.043983444
name2,16590874,0.043934345
提供的示例数据无效,为JSON,但假设已按如下所示进行调整,我们将有:
< sample.json jq -r '[.metric.name] + .values[] | @csv'
"name1",16590879,"0.043984349"
"name1",16590876,"0.043983444"
如果您不需要引号,请使用 join(",")
而不是 @csv
。
sample.json
{
"metric": {
"name": "name1"
},
"values": [
[
16590879,
"0.043984349"
],
[
16590876,
"0.043983444"
]
]
}
有一个 JSON 数据如下
"metric": {
"name" : "name1"
},
"values": [
[
16590879,
"0.043984349"
],
"values": [
[
16590876,
"0.043983444"
]
]
} }
写在 jq 下面,但没有给出正确的结果
jq -r '[.metric.name,(.values[] | map(.) | @csv)'
实际结果
[
"name1",
"16590879",\"0.043984349\"",
"16590876",\"0.043983444\"",
"16590874",\"0.043934345\""
预期结果
name1,16590879,0.043984349
name1,16590876,0.043983444
name2,16590874,0.043934345
提供的示例数据无效,为JSON,但假设已按如下所示进行调整,我们将有:
< sample.json jq -r '[.metric.name] + .values[] | @csv'
"name1",16590879,"0.043984349"
"name1",16590876,"0.043983444"
如果您不需要引号,请使用 join(",")
而不是 @csv
。
sample.json
{
"metric": {
"name": "name1"
},
"values": [
[
16590879,
"0.043984349"
],
[
16590876,
"0.043983444"
]
]
}