我执行了一个 sql 查询,我想将其转换为 json
$QueryResult_uce = Execute-SqlQuery -Server $SQLServer -Database $SQLDBName -UseWindowsAuthentication $True -Username $Username -Password $Password -Query $query_uce;
$QueryResult_uce | $QueryResult_uce 格式表;
我想使用使用 group by 从数据库中提取的数据构建一个 json 这是来自控制台的表数据:
我想构建一个像这样的 json :
{
"date": "20240329",
"indicatorGroupNames": [
"Climate Alignment Scenario"
],
"positions": [
{
"portfolioCode": "6009",
"positionId": 1,
"instrumentId": 3270089,
"weight": 0.00818749252267209
} ,{
"portfolioCode": "6009",
"positionId": 1,
"instrumentId": 4027,
"weight": 0.01818749252267209
}
],
"globalValuation": 9704981607.00212,
}
在哪里
"portfolioCode": corrspond to positions_portfolioCode,
"positionId": corrspond to positions_positionID,
"instrumentId": corrspond to positions_instrumentId,
"weight": corrspond to positions_weight
应分为一组称为位置的组。
我试过
($QueryResult_uce | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors ) | ConvertTo-Json
构建一个 json 但未分组,当我添加时
($QueryResult_uce | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors ) | Group-Object positions_portfolioCode | ForEach{@{$_.Name = $_.Group.Type}} | ConvertTo-Json
这确实给了我一个不好的结果,提前谢谢。
新的
希望回答:旧的和破烂的: