一个json扁平化和数据帧生成工具。
jsonflatten的Python项目详细描述
json文档展平工具
$ jsonflatten --help Usage: jsonflatten [OPTIONS] [JSONFILE] Specify which keys or whole document to flatten Options: -f, --flatten TEXT Flatten only those specified keys generated from `jsoncut -l` option as a comma-separated list or idividually, i.e. `-f7,9` or `-f7 -f9` -n, --nocolor Disable syntax highlighting -q, --quotechar TEXT Quote character used in serialized data, defaults to '"' -s, --slice Disable sequencer --version Show the version and exit. --help Show this message and exit.
安装
$ pip install jsonflatten
用法
- python 3仅支持;jsonflatten当前在python 2下不受支持。
- jsonflatten应该与jsoncut一起使用。
- 通过不设置任何命令行选项来展平整个json文档。
- 使用-f选项展平json文档中的指定键。
- jsonflatten函数也可以导入到您自己的程序或脚本中。
样本数据
- forecast.json-三天天气预报API数据
{
"status":"200",
"datetime": "2017-09-09 11:49",
"request_type": "3-day Forecast",
"list": [
{
"date": "2017-09-09",
"sunrise": "07:04",
"sunset": "19:31",
"moonrise": "22:07",
"moonset": "10:11",
"temp_f_min": 81,
"temp_f_max": 85,
"day": {
"humidity_pct": 82,
"uv_index": 5,
"weather": {
"description": "Heavy Rain/Wind",
"summary": "Tropical storm conditions likely. Rain showers will bring heavy downpours and strong gusty winds at times.",
"icon":"04d"
}, "wind": {
"speed_ave_mph": 49,
"speed_low_mph": 40,
"speed_high_mph": 60,
"direction": "ENE"
}, "rain": {
"precip_pct": 100,
"low_inches": 1,
"high_inches": 2
}
}, "night": {
"humidity_pct": 82,
"weather": {
"description": "Heavy Rain/Wind",
"summary": "Major hurricane conditions likely. Bands of heavy rain containing strong gusty winds at times.",
"icon":"04d"
}, "wind": {
"speed_ave_mph": 120,
"speed_low_mph": 115,
"speed_mph": 130,
"direction": "ENE"
}, "rain": {
"precip_pct": 100,
"low_inches": 5,
"high_inches": 8
}
}
}, {
"date": "2017-10-09",
"sunrise": "07:04",
"sunset": "19:30",
"moonrise": "22:51",
"moonset": "11:12",
"temp_f_min": 79,
"temp_f_max": 85,
"day": {
"humidity_pct": 84,
"uv_index": 5,
"weather": {
"description": "Heavy Rain/Wind",
"summary": "Major hurricane conditions likely. Bands of heavy rain containing strong gusty winds at times.",
"icon":"04d"
}, "wind": {
"speed_ave_mph": 117,
"speed_low_mph": 115,
"speed_mph": 130,
"direction": "SE"
}, "rain": {
"precip_pct": 100,
"low_inches": 1,
"high_inches": 2
}
}, "night": {
"humidity_pct": 83,
"weather": {
"description": "Thunderstorms/Wind",
"summary": "Tropical storm conditions likely. Windy with bands of heavy rain showers and thunderstorms.",
"icon":"04d"
}, "wind": {
"speed_ave_mph": 71,
"speed_low_mph": 60,
"speed_mph": 80,
"direction": "SSW"
}, "rain": {
"precip_pct": 100,
"low_inches": 1,
"high_inches": 2
}
}
}, {
"date": "2017-11-09",
"sunrise": "0705",
"sunset": "1929",
"moonrise": "2339",
"moonset": "1213",
"temp_f_min": 77,
"temp_f_max": 90,
"day": {
"humidity_pct": 72,
"uv_index": 9,
"weather": {
"description": "Partly Cloudy/Wind",
"summary": "Windy. Mostly cloudy skies will become partly cloudy in the afternoon.",
"icon":"03d"
}, "wind": {
"speed_ave_mph": 16,
"speed_low_mph": 10,
"speed_high_mph": 20,
"direction": "WSW"
}, "rain": {
"precip_pct": 20,
"low_inches": 1,
"high_inches": 2
}
}, "night": {
"humidity_pct": 82,
"weather": {
"description": "Partly cloudy",
"summary": "A few clouds.",
"icon":"04d"
}, "wind": {
"speed_ave_mph": 16,
"speed_low_mph": 10,
"speed_mph": 20,
"direction": "WSW"
}, "rain": {
"precip_pct": 10,
"low_inches": null,
"high_inches": null
}
}
}
], "city": {
"id": 4164138,
"name": "Miami",
"coord": {
"lat": 25.7743,
"lon": -80.1937
},
"country": "US"
}
}
展开整个json文档
$ jsonflatten forecast.json
{
"city.coord.lat": 25.7743,
"city.coord.lon": -80.1937,
"city.country": "US",
"city.id": 4164138,
"city.name": "Miami",
"datetime": "2017-09-09 11:49",
"list": [
{
"date": "2017-09-09",
"day.humidity_pct": 82,
"day.rain.high_inches": 2,
"day.rain.low_inches": 1,
"day.rain.precip_pct": 100,
"day.uv_index": 5,
"day.weather.description": "Heavy Rain/Wind",
"day.weather.icon": "04d",
"day.weather.summary": "Tropical storm conditions likely. Rain showers will bring heavy downpours and strong gusty winds at times.",
"day.wind.direction": "ENE",
"day.wind.speed_ave_mph": 49,
"day.wind.speed_high_mph": 60,
"day.wind.speed_low_mph": 40,
"moonrise": "22:07",
"moonset": "10:11",
"night.humidity_pct": 82,
"night.rain.high_inches": 8,
"night.rain.low_inches": 5,
"night.rain.precip_pct": 100,
"night.weather.description": "Heavy Rain/Wind",
"night.weather.icon": "04d",
"night.weather.summary": "Major hurricane conditions likely. Bands of heavy rain containing strong gusty winds at times.",
"night.wind.direction": "ENE",
"night.wind.speed_ave_mph": 120,
"night.wind.speed_low_mph": 115,
"night.wind.speed_mph": 130,
"sunrise": "07:04",
"sunset": "19:31",
"temp_f_max": 85,
"temp_f_min": 81
},
{
"date": "2017-10-09",
"day.humidity_pct": 84,
"day.rain.high_inches": 2,
"day.rain.low_inches": 1,
"day.rain.precip_pct": 100,
"day.uv_index": 5,
"day.weather.description": "Heavy Rain/Wind",
"day.weather.icon": "04d",
"day.weather.summary": "Major hurricane conditions likely. Bands of heavy rain containing strong gusty winds at times.",
"day.wind.direction": "SE",
"day.wind.speed_ave_mph": 117,
"day.wind.speed_low_mph": 115,
"day.wind.speed_mph": 130,
"moonrise": "22:51",
"moonset": "11:12",
"night.humidity_pct": 83,
"night.rain.high_inches": 2,
"night.rain.low_inches": 1,
"night.rain.precip_pct": 100,
"night.weather.description": "Thunderstorms/Wind",
"night.weather.icon": "04d",
"night.weather.summary": "Tropical storm conditions likely. Windy with bands of heavy rain showers and thunderstorms.",
"night.wind.direction": "SSW",
"night.wind.speed_ave_mph": 71,
"night.wind.speed_low_mph": 60,
"night.wind.speed_mph": 80,
"sunrise": "07:04",
"sunset": "19:30",
"temp_f_max": 85,
"temp_f_min": 79
},
{
"date": "2017-11-09",
"day.humidity_pct": 72,
"day.rain.high_inches": 2,
"day.rain.low_inches": 1,
"day.rain.precip_pct": 20,
"day.uv_index": 9,
"day.weather.description": "Partly Cloudy/Wind",
"day.weather.icon": "03d",
"day.weather.summary": "Windy. Mostly cloudy skies will become partly cloudy in the afternoon.",
"day.wind.direction": "WSW",
"day.wind.speed_ave_mph": 16,
"day.wind.speed_high_mph": 20,
"day.wind.speed_low_mph": 10,
"moonrise": "2339",
"moonset": "1213",
"night.humidity_pct": 82,
"night.rain.high_inches": null,
"night.rain.low_inches": null,
"night.rain.precip_pct": 10,
"night.weather.description": "Partly cloudy",
"night.weather.icon": "04d",
"night.weather.summary": "A few clouds.",
"night.wind.direction": "WSW",
"night.wind.speed_ave_mph": 16,
"night.wind.speed_low_mph": 10,
"night.wind.speed_mph": 20,
"sunrise": "0705",
"sunset": "1929",
"temp_f_max": 90,
"temp_f_min": 77
}
],
"request_type": "3-day Forecast",
"status": "200"
}
仅展平特定键
$ cat forecast.json | jsoncut -l 1 city 2 city.coord 3 city.coord.lat 4 city.coord.lon 5 city.country 6 city.id 7 city.name 8 datetime 9 list 10 request_type 11 status
$ cat forecast.json | jsonflatten -f3,4,7 { "city.coord.lat": 25.7743, "city.coord.lon": -80.1937, "city.name": "Miami" }
$ cat forecast.json | jsonflatten -f7 -f9 { "city.name": "Miami", "list": [ { "date": "2017-09-09", "day.humidity_pct": 82, "day.rain.high_inches": 2, "day.rain.low_inches": 1, "day.rain.precip_pct": 100, "day.uv_index": 5, "day.weather.description": "Heavy Rain/Wind", "day.weather.icon": "04d", "day.weather.summary": "Tropical storm conditions likely. Rain showers will bring heavy downpours and strong gusty winds at times.", "day.wind.direction": "ENE", "day.wind.speed_ave_mph": 49, "day.wind.speed_high_mph": 60, "day.wind.speed_low_mph": 40, "moonrise": "22:07", "moonset": "10:11", "night.humidity_pct": 82, "night.rain.high_inches": 8, "night.rain.low_inches": 5, "night.rain.precip_pct": 100, "night.weather.description": "Heavy Rain/Wind", "night.weather.icon": "04d", "night.weather.summary": "Major hurricane conditions likely. Bands of heavy rain containing strong gusty winds at times.", "night.wind.direction": "ENE", "night.wind.speed_ave_mph": 120, "night.wind.speed_low_mph": 115, "night.wind.speed_mph": 130, "sunrise": "07:04", "sunset": "19:31", "temp_f_max": 85, "temp_f_min": 81 }, { "date": "2017-10-09", "day.humidity_pct": 84, "day.rain.high_inches": 2, "day.rain.low_inches": 1, "day.rain.precip_pct": 100, "day.uv_index": 5, "day.weather.description": "Heavy Rain/Wind", "day.weather.icon": "04d", "day.weather.summary": "Major hurricane conditions likely. Bands of heavy rain containing strong gusty winds at times.", "day.wind.direction": "SE", "day.wind.speed_ave_mph": 117, "day.wind.speed_low_mph": 115, "day.wind.speed_mph": 130, "moonrise": "22:51", "moonset": "11:12", "night.humidity_pct": 83, "night.rain.high_inches": 2, "night.rain.low_inches": 1, "night.rain.precip_pct": 100, "night.weather.description": "Thunderstorms/Wind", "night.weather.icon": "04d", "night.weather.summary": "Tropical storm conditions likely. Windy with bands of heavy rain showers and thunderstorms.", "night.wind.direction": "SSW", "night.wind.speed_ave_mph": 71, "night.wind.speed_low_mph": 60, "night.wind.speed_mph": 80, "sunrise": "07:04", "sunset": "19:30", "temp_f_max": 85, "temp_f_min": 79 }, { "date": "2017-11-09", "day.humidity_pct": 72, "day.rain.high_inches": 2, "day.rain.low_inches": 1, "day.rain.precip_pct": 20, "day.uv_index": 9, "day.weather.description": "Partly Cloudy/Wind", "day.weather.icon": "03d", "day.weather.summary": "Windy. Mostly cloudy skies will become partly cloudy in the afternoon.", "day.wind.direction": "WSW", "day.wind.speed_ave_mph": 16, "day.wind.speed_high_mph": 20, "day.wind.speed_low_mph": 10, "moonrise": "2339", "moonset": "1213", "night.humidity_pct": 82, "night.rain.high_inches": null, "night.rain.low_inches": null, "night.rain.precip_pct": 10, "night.weather.description": "Partly cloudy", "night.weather.icon": "04d", "night.weather.summary": "A few clouds.", "night.wind.direction": "WSW", "night.wind.speed_ave_mph": 16, "night.wind.speed_low_mph": 10, "night.wind.speed_mph": 20, "sunrise": "0705", "sunset": "1929", "temp_f_max": 90, "temp_f_min": 77 } ] }
学分
brian petersonbpeterso2000,json转换的创建者https://github.com/json-transformations