Skip to main content

Restructure JSON body into hashtable

This script is needed if you get an object by any source (e.g., json) and you have to give every member of the object as a value (keypair) into a hashtable.

$bodyjson = @"
{
    "logtype": "testlogs2",
    "logbody": {
        "computername":"Device-123456",
        "user":"luca",
        "ouput":"registry key was set",
        "status":"success",
        "time":"xyz"
    }
}
"@

$bodyobject = ConvertFrom-Json $bodyjson
$logtype = $bodyobject.logtype
$logbodyobject = $bodyobject.logbody

$logbodyobjectmember = Get-Member -InputObject $logbodyobject | where {$_.Membertype -eq "NoteProperty"}

$outputproperties = @{}

foreach ($item in $logbodyobjectmember.name){
    $outputproperties.add($item, $logbodyobject.$item)
}