我一直在努力理解 jq 語法,但我遇到了問題。我正在嘗試像其他人一樣將 json 轉換為 csv。我在論壇上找到了很多條目,但似乎沒有一個適合我。當我縮小過濾器的范圍時,我總是會得到一些錯誤或部分結果。
目標:
- 我使用 https://api.weather.com/v2/pws/history/hourly?stationId=IBONIE3&format=json&units=m&date=20210731&apiKey=從https://www.wunderground.com/獲取資料
- 我想將此資料轉換為 csv
我從源頭得到的是 1 小時內 1 天的資料。資料存盤在“觀察”下。看起來像這樣:
{"observations":[
{
"stationID": "IBONIE3",
"tz": "Europe/Warsaw",
"obsTimeUtc": "2021-07-31T21:59:50Z",
"obsTimeLocal": "2021-07-31 23:59:50",
"epoch": 1627768790,
"lat": 52.203785,
"lon": 20.618021,
"solarRadiationHigh": 0,
"uvHigh": 0,
"winddirAvg": 257,
"humidityHigh": 74,
"humidityLow": 71,
"humidityAvg": 71,
"qcStatus": 1,
"metric": {
"tempHigh": 20,
"tempLow": 20,
"tempAvg": 20,
"windspeedHigh": 8,
"windspeedLow": 0,
"windspeedAvg": 2,
"windgustHigh": 12,
"windgustLow": 0,
"windgustAvg": 3,
"dewptHigh": 15,
"dewptLow": 15,
"dewptAvg": 15,
"windchillHigh": 20,
"windchillLow": 20,
"windchillAvg": 20,
"heatindexHigh": 20,
"heatindexLow": 20,
"heatindexAvg": 20,
"pressureMax": 994.58,
"pressureMin": 993.91,
"pressureTrend": 0,
"precipRate": 0,
"precipTotal": 0
}
}]}
顯然,這只是單個檔案中 24 個條目中的 1 個條目。
當我這樣做時:
cat file.json | jq '.observations[0],.observations[0].metric | keys_unsorted | @csv'
"\"stationID\",\"tz\",\"obsTimeUtc\",\"obsTimeLocal\",\"epoch\",\"lat\",\"lon\",\"solarRadiationHigh\",\"uvHigh\",\"winddirAvg\",\"humidityHigh\",\"humidityLow\",\"humidityAvg\",\"qcStatus\",\"metric\""
"\"tempHigh\",\"tempLow\",\"tempAvg\",\"windspeedHigh\",\"windspeedLow\",\"windspeedAvg\",\"windgustHigh\",\"windgustLow\",\"windgustAvg\",\"dewptHigh\",\"dewptLow\",\"dewptAvg\",\"windchillHigh\",\"windchillLow\",\"windchillAvg\",\"heatindexHigh\",\"heatindexLow\",\"heatindexAvg\",\"pressureMax\",\"pressureMin\",\"pressureTrend\",\"precipRate\",\"precipTotal\""
我確實得到了正確格式的標題,但是當我這樣做時
cat file.json | jq -r '.observations[] | map(values) | @csv
jq: error (at <stdin>:1): object ({"tempHigh"...) is not valid in a csv row
我得到錯誤。這對我來說很明顯,因為它進入了稱為 metric 的子陣列,這是顯示錯誤的地方。我只能通過運行這個來獲得這些指標:
cat file.json | jq -r '.observations[].metric | map(values) | @csv
20,18,19,0,0,0,0,0,0,15,14,14,20,18,19,20,18,19,994.58,994.24,0.34,0,0
跳過所有其他資料,但這不是我想要的。
我如何將此度量陣列轉換為非陣列物件?有沒有辦法在單個查詢中做到這一點?
顯然,標題也不能有變數“度量”,而是該子陣列中的所有專案 - 度量。我可以手動修復標題甚至跳過它,但是如何獲取整個資料,而不僅僅是指標?
uj5u.com熱心網友回復:
如果沒有鍵沖突,您可以將.metric子陣列的專案集成到實際記錄中:
jq -r '
.observations[] |= (. .metric | del(.metric))
| (.observations[0] | keys_unsorted), (.observations[] | map(values))
| @csv
' file.json
"stationID","tz","obsTimeUtc","obsTimeLocal","epoch","lat","lon","solarRadiationHigh","uvHigh","winddirAvg","humidityHigh","humidityLow","humidityAvg","qcStatus","tempHigh","tempLow","tempAvg","windspeedHigh","windspeedLow","windspeedAvg","windgustHigh","windgustLow","windgustAvg","dewptHigh","dewptLow","dewptAvg","windchillHigh","windchillLow","windchillAvg","heatindexHigh","heatindexLow","heatindexAvg","pressureMax","pressureMin","pressureTrend","precipRate","precipTotal"
"IBONIE3","Europe/Warsaw","2021-07-31T21:59:50Z","2021-07-31 23:59:50",1627768790,52.203785,20.618021,0,0,257,74,71,71,1,20,20,20,8,0,2,12,0,3,15,15,15,20,20,20,20,20,20,994.58,993.91,0,0,0
演示
如果它們碰巧發生碰撞,請在它們的名稱中添加一些前綴以消除歧義:
jq -r '
.observations[] |= (. (.metric | with_entries(.key |= "metric_\(.)")) | del(.metric))
| (.observations[0] | keys_unsorted), (.observations[] | map(values))
| @csv
' file.json
"stationID","tz","obsTimeUtc","obsTimeLocal","epoch","lat","lon","solarRadiationHigh","uvHigh","winddirAvg","humidityHigh","humidityLow","humidityAvg","qcStatus","metric_tempHigh","metric_tempLow","metric_tempAvg","metric_windspeedHigh","metric_windspeedLow","metric_windspeedAvg","metric_windgustHigh","metric_windgustLow","metric_windgustAvg","metric_dewptHigh","metric_dewptLow","metric_dewptAvg","metric_windchillHigh","metric_windchillLow","metric_windchillAvg","metric_heatindexHigh","metric_heatindexLow","metric_heatindexAvg","metric_pressureMax","metric_pressureMin","metric_pressureTrend","metric_precipRate","metric_precipTotal"
"IBONIE3","Europe/Warsaw","2021-07-31T21:59:50Z","2021-07-31 23:59:50",1627768790,52.203785,20.618021,0,0,257,74,71,71,1,20,20,20,8,0,2,12,0,3,15,15,15,20,20,20,20,20,20,994.58,993.91,0,0,0
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/440661.html
