我正在嘗試將下面的 Json 表轉換為 Json 陣列物件,下面的 json 失敗,因為有單個嵌套 json 物件,但是當嵌套 json 中有多個物件時它可以作業
[
{
"name": "PrimaryResult",
"columns": [
{
"name": "Computer",
"type": "string"
},
{
"name": "LastHeartbeat",
"type": "datetime"
}
],
"rows": [
[
"xxxxxxx.dev.org",
"2022-01-19T04:49:48.937Z"
]
]
}
]
預期輸出如下
[
{
"Computer": xxxxxxx.dev.org",
"LastHeartbeat": "2022-01-19T04:49:48.937Z"
}
]
我嘗試使用下面的 Json 腳本,當陣列中有多個物件時它可以作業
[
{
"name": "PrimaryResult",
"columns": [
{
"name": "Computer",
"type": "string"
},
{
"name": "LastHeartbeat",
"type": "datetime"
}
],
"rows": [
[
"zzzzz.test.org",
"2022-01-04T09:06:45.44Z"
],
[
"yyyy.dev.org",
"2022-01-04T09:06:30.233Z"
],
[
"xxxx.prod.org",
"2022-01-04T09:06:08.893Z"
],
[
"xxxx.dev.org",
"2022-01-04T09:06:04.667Z"
]
]
}
]
I have tried with below powershell script
=============================================
$TriggerMetadata = Get-Content .\test4.json | ConvertFrom-Json
$affected_resources = $TriggerMetadata.tables
$resources =
ForEach($Row in $affected_resources.rows)
{
$TmpHash = [Ordered]@{}
For($i = 0; $i -lt $Row.Length; $i )
{
$TmpHash.Add($affected_resources.columns.name[$i], $Row[$i] )
}
[PSCustomObject]$TmpHash
}
$body = $resources | ConvertTo-Json -Depth 100
$Body
低于錯誤
Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key"
At line:22 char:13
$TmpHash.Add($affected_resources.columns.name[$i], $Row[$ ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : ArgumentNullException
uj5u.com熱心網友回復:
當陣列中只有一個元素時,PowerShell 會“解開”陣列。
在這種情況下會產生您遇到的錯誤,您可以使用, 一元逗號運算子來修復,該運算子會將輸入陣列的單個元素包裝到另一個單個元素陣列中。
當 PowerShell 解包時,結果是 json 檔案中的原始行值陣列
假設您已將 JSON 轉換為變數$json:
$headers = $json.columns.name
# if there is only one 'rows 'element, wrap it in an array by prefixing with comma
if ($json.rows[0] -is [array]) { $rows = $json.rows } else { $rows = ,$json.rows }
$resources = $rows | ForEach-Object {
$hash = [ordered]@{}
for ($i = 0; $i -lt $headers.Count; $i ) {
$hash[$headers[$i]] = $_[$i]
}
[PsCustomObject]$hash
}
輸出:
{
"Computer": "zzzzz.test.org",
"LastHeartbeat": "2022-01-04T09:06:45.44Z"
}
如果您確實需要方括號,您可以像以前一樣進行測驗并將結果括在 '[..]'
if ($json.rows[0] -is [array]) {
$body = $resources | ConvertTo-Json -Depth 100
}
else {
$body = "[ {0} ]" -f ($resources | ConvertTo-Json -Depth 100)
}
輸出:
[ {
"Computer": "zzzzz.test.org",
"LastHeartbeat": "2022-01-04T09:06:45.44Z"
} ]
uj5u.com熱心網友回復:
這是我之前回答的改編,應該能夠處理這個問題上提出的兩個 Json。
function Parse-Json {
param(
[parameter(ValueFromPipeline)]
[object]$InputObject
)
process
{
$columns = $InputObject.Columns.Name
foreach($row in $InputObject.Rows)
{
$out = [ordered]@{}; $i = 0
foreach($element in $row)
{
$out[$columns[$i ]] = $element
}
[pscustomobject]$out
}
}
}
該函式可以從管道中獲取輸入,因此您可以將 Json 字串通過管道傳輸ConvertFrom-Json到該函式中:
PS /> $json1 | ConvertFrom-Json | Parse-Json
Computer LastHeartbeat
-------- -------------
xxxxxxx.dev.org 1/19/2022 4:49:48 AM
PS /> $json2 | ConvertFrom-Json | Parse-Json
Computer LastHeartbeat
-------- -------------
zzzzz.test.org 1/4/2022 9:06:45 AM
yyyy.dev.org 1/4/2022 9:06:30 AM
xxxx.prod.org 1/4/2022 9:06:08 AM
xxxx.dev.org 1/4/2022 9:06:04 AM
它也適用于多個 Json:
PS /> $json1, $json2 | ConvertFrom-Json | Parse-Json
Computer LastHeartbeat
-------- -------------
xxxxxxx.dev.org 1/19/2022 4:49:48 AM
zzzzz.test.org 1/4/2022 9:06:45 AM
yyyy.dev.org 1/4/2022 9:06:30 AM
xxxx.prod.org 1/4/2022 9:06:08 AM
xxxx.dev.org 1/4/2022 9:06:04 AM
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/416702.html
標籤:
上一篇:批量洗掉資料庫記錄
