I'm having trouble with adding hashtables to a multidimensional array. I coded the following:
$Data = @{BIBs = @(
@{$BIB = @{BIBName=$BIBName},
@{Standort = $Standort},
@{Bücher = @(
@{BuchName = $BuchName;
Autor = $Autor
})
}}
)}
This code is functioning and creates an output, which I store in a JSON:
{
"BIBs": [
{
"BIB1": [
{
"BIBName": "123"
},
{
"Standort": "123"
},
{
"Bücher": [
{
"Autor": "123",
"BuchName": "123"
}
]
}
]
},
{
"BIB2": [
{
"BIBname": "345"
},
{
"Standort": "345"
},
{
"Bücher": [
{
"Autor": "345",
"Buchname": "345"
}
]
}
]
}
]
}
I have extra code which adds another hashtable to array "BIBs" as you can see.
$jsonfile = "C:SkripteibV2-1000.json"
$Data = Get-Content $jsonfile | ConvertFrom-Json
$Data.BIBs += New-Object -TypeName PSObject -Property @{
$BIB = @{BIBname=$BIBName}, @{Standort=$Standort},
@{Bücher = @(@{Buchname=$BuchName;Autor=$Autor})}
}
When the output is like above, I'm not able to add another hashtable to "Bücher". I checked the type of "Bücher" with
$data.BIBs.BIB1.Bücher.GetType()
and it's actually an array:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
I tried
$Data.BIBs.BIB1.Bücher += @{Person="Max";Alter="35"}
to add a new hashtable, like I did with "BIB2", but I am getting the error:
The property 'Bücher' cannot be found on this object. Verify that the property
exists and can be set.
At line:5 char:1
+ $data.BIBs.BIB1.Bücher += @{Motor="asdf";pers="345"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Do you know how I add @{Person="Max";Alter="35"}
to "Bücher"?
See Question&Answers more detail:
os