Search code examples
jsonpowershellnew-webserviceproxy

ConvertFrom-JSON won't accept convertto-json with children when working with WebServiceProxy


I am pulling data from an API using the New-WebServiceProxy in PowerShell 4.0 and then piping it out to a JSON file for review and import on another API service (same API version, etc, just a different host).

$tasklist.Taskconfig | ConvertTo-JSON-Depth 50 -As String | Out-File -FilePath $exportpath\$name.xml -Force

Gives me my XML containing the TaskConfig. In this case, TaskConfig is an object type automatically generated by the API I'm interfacing with. When I want to import the content I am using:

$taskconfig      = (Get-Content "$taskjson") -join "`n" | ConvertFrom-Json

but when I run this it's unable to create the object. I assume this is because the JSON contains nested children, giving the error-

Cannot convert value "@{Name=plugindive; Value=;> Children=System.Object[]}" to type "Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig". Error: "Cannot convert the "@{Name=plugindive; Value=;Children=System.Object[]}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig"."

I've tried explictly stating the type of object:

$taskconfig      = [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig](Get-Content "$taskjson" | Out-string | ConvertFrom-Json)

as well as creating the object then trying to add the children from my JSON -

$taskconfig.children = $json.children

But these all fail in the same way.

I don't seem to get this same issue in PowerShell 5.0 interestingly enough, but I can't verify why - is there another way to approach this?

Added example JSON below

{"Name": "plugindive",
"Value": null,
"Children": [{
        "Name": "auto",
        "Value": "False",
        "Children": [

        ]
    },
    {
        "Name": "categories",
        "Value": null,
        "Children": [{
                "Name": "Module Z",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module A",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module B",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module C",
                "Value": "False",
                "Children": [

                ]
            }
        ]
    }
]
}

Solution

  • It seems as if this doesn't work in PowerShell v3.0, so I simply ended up making posts with the explicit XML directly, rather than converting to JSON.