我有一个名为“test.json”的json文件,如下所示,并希望将其转换为CSV文件。我尝试过python pandas,但遗憾的是没有成功。 任何人的帮助都会非常感激。
{
"epoch": 1487237243.000440,
"comment": null,
"header": "mac[#mode|_rate] : latitude longitude range max age sig_A noise_A sig_B noise_B num loss kBps",
"data": {
"04:e5:48:01:73:29#0000": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.00, -101.00, -96.00, -98.00, 2, 0, 0.138],
"04:e5:48:01:73:29#0001": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.50, -101.00, -96.50, -102.50, 2, 0, 0.438],
"04:e5:48:01:73:29#0002": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.50, -99.50, -96.00, -101.00, 2, 0, 1.538],
"04:e5:48:01:73:29#0003": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.00, -101.00, -97.00, -101.00, 1, 0, 0.138],
"04:e5:48:01:73:29#0004": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.00, -101.00, -96.00, -101.00, 1, 0, 0.438],
"04:e5:48:01:73:29#0005": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.00, -101.00, -95.00, -98.00, 1, 0, 1.538],
"04:e5:48:01:73:29#0006": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.00, -101.00, -95.00, -98.00, 1, 0, 0.138],
"04:e5:48:01:73:29#0007": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.00, -101.00, -96.50, -101.00, 2, 0, 0.438],
"04:e5:48:01:73:29#0008": [48.1499800, 11.5833270, -1.00, 0.00, 0.00, -80.00, -101.00, -96.50, -99.50, 2, 0, 1.538]
}
}
答案 0 :(得分:1)
以下是使用jq的解决方案。
如果filter.jq
包含以下过滤器
[ .header | match("[^: ]+";"g") | .string ]
, ( .data | keys[] as $k | [$k] + .[$k] )
| @csv
和data.json
包含示例数据
$ jq -M -r -f filter.jq data.json
产生
"mac[#mode|_rate]","latitude","longitude","range","max","age","sig_A","noise_A","sig_B","noise_B","num","loss","kBps"
"04:e5:48:01:73:29#0000",48.14998,11.583327,-1,0,0,-80,-101,-96,-98,2,0,0.138
"04:e5:48:01:73:29#0001",48.14998,11.583327,-1,0,0,-80.5,-101,-96.5,-102.5,2,0,0.438
"04:e5:48:01:73:29#0002",48.14998,11.583327,-1,0,0,-80.5,-99.5,-96,-101,2,0,1.538
"04:e5:48:01:73:29#0003",48.14998,11.583327,-1,0,0,-80,-101,-97,-101,1,0,0.138
"04:e5:48:01:73:29#0004",48.14998,11.583327,-1,0,0,-80,-101,-96,-101,1,0,0.438
"04:e5:48:01:73:29#0005",48.14998,11.583327,-1,0,0,-80,-101,-95,-98,1,0,1.538
"04:e5:48:01:73:29#0006",48.14998,11.583327,-1,0,0,-80,-101,-95,-98,1,0,0.138
"04:e5:48:01:73:29#0007",48.14998,11.583327,-1,0,0,-80,-101,-96.5,-101,2,0,0.438
"04:e5:48:01:73:29#0008",48.14998,11.583327,-1,0,0,-80,-101,-96.5,-99.5,2,0,1.538
答案 1 :(得分:0)
使用jsonlite
和dplyr
的其他解决方案:
install.packages(c("jsonlite","dplyr"))
library(jsonlite)
library(dplyr)
# asumme your file is "my_file.json"
my_data <- as_tibble(fromJSON("my_file.json"))