I'm trying pull data from WhatConverts via their API, and to then upload that into a SQL table. As I've dug deeper into the nature of the underlying data, there are several data types being returned as objects; I only know upfront the outer data element type will be an object, but the nested element names, and types in the nested object array are totally variable. The returned data is json.
My plan was to import the typed data into SQL, and for the nested data, to try and convert the nesting into XML and import that as XML data into SQL for possible future processing based on the end user's requirements. I'm grooming this into a DataTable from the Invoke-RestMethod object prior to my SQL import. I can't handle the nested data, here is where I'm at:
#NOTE $importedData is an array of PSCustomObject returned from Invoke-RestMethod
$manipulatedData = New-Object System.Data.DataTable
$importedData | ForEach-Object {
$newRow = $manipulatedData.NewRow()
foreach ($prop in $_.PSObject.Properties) {
# nested object
if ($prop.Value -is [System.Management.Automation.PSCustomObject]) {
$tempPropname = $prop.Name
if (-not ($manipulatedData.Columns.Contains($tempPropname))) {
$newCol = New-Object System.Data.DataColumn($tempPropname)
$manipulatedData.Columns.Add($newCol, "System.String" ) | Out-Null
}
#$prop | Select-Object Name,Value | ConvertTo-Xml -As String
$newRow.$tempPropname = $prop | Select-Object Name,Value | ConvertTo-Xml -As String
}
# primitive value
else {
$tempPropname = $prop.Name
if (-not ($manipulatedData.Columns.Contains(($tempPropname)))) {
$newCol = New-Object System.Data.DataColumn($tempPropname)
$manipulatedData.Columns.Add($newCol, $prop.TypeNameOfValue ) | Out-Null
}
if ($prop.Value -ne '{}') {
$newRow.$tempPropname = $prop.Value
}
}
}
$manipulatedData.Rows.Add($newRow)
}
For this exercise I believe I need to iterate through each of the nested property elements (I only care about 1 level deep), but then I got stuck having to build out an XML document from scratch so I changed direction and tried just exporting the whole object to XML.
The results are garbage, here is the output:
SetValueInvocationException:
Line |
90 | $newRow.$tempPropname = $prop | Select-Object Name,Value …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception setting "additional_fields": "Type of value has a mismatch with column typeCouldn't store <<?xml version="1.0" encoding="utf-8"?> <Objects> <Object Type="System.Management.Automation.PSCustomObject"> <Property Name="Name"
| Type="System.String">additional_fields</Property> <Property Name="Value" Type="System.Management.Automation.PSCustomObject"> <Property Type="System.String">@{Junk?=No; New Client?=Not Set; Service-Based?=Not Set}</Property> <Property Name="Junk?"
| Type="System.Management.Automation.PSNoteProperty">No</Property> <Property Name="New Client?" Type="System.Management.Automation.PSNoteProperty">Not Set</Property> <Property Name="Service-Based?" Type="System.Management.Automation.PSNoteProperty">Not
| Set</Property> </Property> </Object> </Objects>> in additional_fields Column. Expected type is Object[]."