I need to convert some json containing forecast data to csv (for use in gnuplot). Have tried a couple of json2csv utilities
json sample:
{"cod":"200","message":0.006,"cnt":40,"list":[{"dt":1519333200,"main":{"temp":271.62,"temp_min":271.62,"temp_max":272.921,"pressure":1028.3,"sea_level":1037.2,"grnd_level":1028.3,"humidity":88,"temp_kf":-1.3},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.86,"deg":78.004},"rain":{},"snow":{},"sys":{"pod":"n"},"dt_txt":"2018-02-22 21:00:00"},{"dt":1519344000,"main":{"temp":271.22,"temp_min":271.22,"temp_max":272.193,"pressure":1028.11,"sea_level":1037.04,"grnd_level":1028.11,"humidity":100,"temp_kf":-0.98},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.52,"deg":80.0016},"rain":{},"snow":{},"sys":{"pod":"n"},"dt_txt":"2018-02-23 00:00:00"},
Same pretty-printed:
{
"cod": "200",
"message": 0.006,
"cnt": 40,
"list": [
{
"dt": 1519333200,
"main": {
"temp": 271.62,
"temp_min": 271.62,
"temp_max": 272.921,
"pressure": 1028.3,
"sea_level": 1037.2,
"grnd_level": 1028.3,
"humidity": 88,
"temp_kf": -1.3
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 4.86,
"deg": 78.004
},
"rain": {},
"snow": {},
"sys": {
"pod": "n"
},
"dt_txt": "2018-02-22 21:00:00"
},
{
"dt": 1519344000,
"main": {
"temp": 271.22,
"temp_min": 271.22,
"temp_max": 272.193,
"pressure": 1028.11,
"sea_level": 1037.04,
"grnd_level": 1028.11,
"humidity": 100,
"temp_kf": -0.98
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 4.52,
"deg": 80.0016
},
"rain": {},
"snow": {},
"sys": {
"pod": "n"
},
"dt_txt": "2018-02-23 00:00:00"
},
I've managed to use json2csv to flatten the data (and cut the first few unneeded fields):
1519333200 271.62 271.62 272.921 1028.3 1037.2 1028.3 88 -1.3 800 "Clear" "clear sky" "01n" 0 4.86 78.004 "{}" "{}" "n"
"2018-02-22 21:00:00" 1519344000 271.22 271.22 272.193 1028.11 1037.04 1028.11 100 -0.98 800 "Clear" "clear sky" "01n" 0 4.52 80.0016 "{}" "{}"
"n" "2018-02-23 00:00:00"
I need either creating the new lines after the plain text date (sed??) or ideally flattening the json directly to csv
jq -r '.list[]|[.dt, .main.temp]|@csv' fileand then feed them directly into Gnuplot.