JMESPath 很好用,但還是有先天上的限制,線上稍早前發的 issue,確定 JMESPath 不支援 groupby + sum 功能
那只好依靠其他工具來達成目的了
How JMESPath group by and summary data · Issue #70 · jmespath/jmespath.js
這邊開始使用 Visaul Studio + PowerShell + Azure CLI 來開發分析報表的開發
VSCode + PowerShell 整合的很好,有以下優點 :
寫這篇時候,想時候 ConvertFrom-Json | Group
達到分析目的
結果遇到奇怪現象 “$g = $json | ConvertFrom-Json | Group State”
結果跟 “$g = $json | ConvertFrom-Json ; $g = $g | Group State ;”
不一樣
完整例子 :
$data = @(
[PSCustomObject]@{State="TAIPEI";Type="1"}
[PSCustomObject]@{State="TAIPEI";Type="2"}
[PSCustomObject]@{State="KH";Type="3"}
[PSCustomObject]@{State="KH";Type="4"}
[PSCustomObject]@{State="KH";Type="5"}
[PSCustomObject]@{State="TX";Type="6"}
[PSCustomObject]@{State="TX";Type="7"}
)
$json = $data | ConvertTo-Json ;
$g = $data | Group State;
Write-Host($g.Length); #result : 3
$g = $json | ConvertFrom-Json | Group State ;
Write-Host($g.Length); #result : 1
$g = $json | ConvertFrom-Json ;
$g = $g | Group State ;
Write-Host($g.Length); #result : 3
發問在 S.O 有大神指出這算是小坑,PowerShell 5
版本 ConvertFrom-JSON | Group
不會轉成System.Management.Automation.PSCustomObject
,在PowerShell 7
版本才會。
解決方式 : 手動指定轉型 | %{[PsCustomObject]$_}
$g = $json | ConvertFrom-Json | %{[PsCustomObject]$_} | Group State ;
Write-Host($g.Length); #result : 3
TODO:研究 powershell 執行 url ,方便做一個範本給讀者使用