I am trying to convert below Json table to Json object
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "TimeGenerated",
"type": "datetime"
},
{
"name": "Computer",
"type": "string"
},
{
"name": "EventID",
"type": "int"
},
{
"name": "EventLevelName",
"type": "string"
},
{
"name": "RenderedDescription",
"type": "string"
}
],
"rows": [
[
"2022-01-10T09:25:41.837Z",
"Mycomputer",
2974,
"Error",
"The attribute value provided is not unique in the forest or partition. Attribute: servicePrincipalName Value=MSSQL/xxxxxxx.dev.com CN=xxxxxxxxx,OU=DBS,OU=SVC,OU=Servers,OU=UK,DC=dev,DC=xxxxx,DC=xxxxxx,DC=org Winerror: 8647 See http://go.microsoft.com/fwlink/?LinkID=279782 for more details on this policy. "
]
]
}
]
Expected output is as below, i have tried hard but no found how to do it
[
{
"TimeGenerated": "2022-01-10T09:25:41.837Z",
"Computer": "Mycomputer",
"EventID": "2974",
"EventLevelName": "Error",
"Rendered Description": "The attribute value provided is not unique in the forest or partition. Attribute: servicePrincipalName Value=MSSQL/xxxxxxx.dev.com CN=xxxxxxxxx,OU=DBS,OU=SVC,OU=Servers,OU=UK,DC=dev,DC=xxxxx,DC=xxxxxx,DC=org Winerror: 8647 See http://go.microsoft.com/fwlink/?LinkID=279782 for more details on this policy."
}
]
I have tried below script but not worked as expected, Need to get result as mentioned above
$affected_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
}
CodePudding user response:
This worked for me adding a new set of rows, it should scale fine with more rows however it's hard to tell without testing it more.
$columns = $json.tables.columns.Name
$newObject = $json.tables.rows.ForEach({
begin {
$i = 0
$out = [ordered]@{}
}
process {
$out[$columns[$i ]] = $_
if($i -eq $columns.count)
{
[pscustomobject]$out
$out = [ordered]@{}
$i = 0
}
}
})
$newObject | ConvertTo-Json
- Example of
$newObject | ConvertTo-Json:
[
{
"TimeGenerated": "2022-01-10T09:25:41.837Z",
"Computer": "Mycomputer",
"EventID": 2974,
"EventLevelName": "Error",
"RenderedDescription": "The attribute value provided is...."
},
{
"TimeGenerated": "2022-02-10T09:25:41.837Z",
"Computer": "Mycomputer2",
"EventID": 1234,
"EventLevelName": "Error2TestTest",
"RenderedDescription": "The attribute value provided is...."
}
]
