我正在尝试使用 jq 将 JSON 输出转换为 CSV。
这是 input.json (精简了很多 - 我最多可能有 600 个数组元素,但每个元素的数量相同):
{
"latitude": [39.582, 39.582, 39.582],
"longitude": [26.675, 26.675, 26.675],
"drivingDifficultyIndex": [0, 34, 34],
"iconCode": [31, 11, 11],
"observationTimeUtcIso": ["2016-06-26T00:20:00+0000", "2016-06-26T01:20:00+0000", "2016-06-26T02:20:00+0000"],
"precip1Hour": [0.0, 0.1, 0.5]
}
Run Code Online (Sandbox Code Playgroud)
迄今为止我最好的尝试是:
jq --raw-output 'keys , .[] | @csv'
Run Code Online (Sandbox Code Playgroud)
这使得
"drivingDifficultyIndex","iconCode","latitude","longitude","observationTimeUtcIso","precip1Hour"
39.582,39.582,39.582
26.675,26.675,26.675
0,34,34
31,11,11
"2016-06-26T00:20:00+0000","2016-06-26T01:20:00+0000","2016-06-26T02:20:00+0000"
0,0.1,0.5
Run Code Online (Sandbox Code Playgroud)
如何使用 jq 将任意简单的 JSON 转换为 CSV?给出了一些很好的提示,但我最终仍然得到行中的数据(如上所示)而不是列中。
我追求的是这样的:
"latitude","longitude","drivingDifficultyIndex","iconCode","observationTimeUtcIso","precip1Hour"
39.582, 26.675, 0, 31, \2016-06-26T00:20:00+0000\", 0
39.582, 26.675, 34, 11, \"2016-06-26T01:20:00+0000\", 0.1
39.582, 26.675, 34, 11, …
Run Code Online (Sandbox Code Playgroud)