Reputation: 320
In preparing body data for use in an Invoke-RestMethod
call, I create a PSCustomObject with the intent to convert it to JSON - this has the benefit of allowing me create my data object in a clear and concise manner, and then have ConvertTo-JSON
handle the JSON formatting for me.
Anyway, I notice that one object in particular tends to have issues in the sense that Powershell (or the underlying .NET class?) is inserting what appears to be debug info into the resulting JSON object.
PSCustomObject Code
$importCommitBodyObj = [PSCustomObject]@{
delete_after_import = "0"
file_description = $null
file_title = $null
import_source = @{name="Me"; status=@{name="Shareable"}}
file_type = "6"
global_status = "1"
apply_attributes = {@{name="Family"; value="Smith"; sources={@{name="Me"; status=@{name="Shareable"}}}}}
}
PSCustomObject as viewed in the console
PS C:\> $importCommitBodyObj
delete_after_import : 0
file_description :
file_title :
import_source : {name, status}
file_type : 6
global_status : 1
apply_attributes : @{name="Family"; value="Smith"; sources={@{name="Me"; status=@{name="Shareable"}}}}
Object after conversion to JSON
PS C:\> $importCommitBodyObj | ConvertTo-Json
{
"delete_after_import": "0",
"file_description": null,
"file_title": null,
"import_source": {
"name": "Me",
"status": {
"name": "Shareable"
}
},
"file_type": "6",
"global_status": "1",
"apply_attributes": {
"Attributes": [
],
"File": null,
"IsFilter": false,
"IsConfiguration": false,
"Module": null,
"StartPosition": {
"Content": "{@{name=\"Family\"; value=\"Smith\"; sources={@{name=\"Me\"; status=@{name=\"Shareable\"}}}}}",
"Type": 19,
"Start": 377,
"Length": 85,
"StartLine": 8,
"StartColumn": 38,
"EndLine": 8,
"EndColumn": 123
},
"DebuggerHidden": false,
"Id": "0fae92d5-6e02-472c-b040-a6bcbb495c6d",
"Ast": {
"Attributes": "",
"UsingStatements": "",
"ParamBlock": null,
"BeginBlock": null,
"ProcessBlock": null,
"EndBlock": "@{name=\"Family\"; value=\"Smith\"; sources={@{name=\"Me\"; status=@{name=\"Shareable\"}}}}",
"DynamicParamBlock": null,
"ScriptRequirements": null,
"Extent": "{@{name=\"Family\"; value=\"Smith\"; sources={@{name=\"Me\"; status=@{name=\"Shareable\"}}}}}",
"Parent": "{@{name=\"Family\"; value=\"Smith\"; sources={@{name=\"Me\"; status=@{name=\"Shareable\"}}}}}"
}
}
}
What I expect the output to look like
{
"delete_after_import": 0,
"file_description": null,
"file_title": null,
"import_source": {
"name": "Me",
"status": {
"name": "Shareable"
}
},
"file_type": 6,
"global_status": 1,
"apply_attributes": [
{
"name": "Family",
"value": "Smith",
"sources": [
{
"name": "Me",
"status": {
"name": "Shareable"
}
}
]
}
]
}
I've never seen ConvertTo-JSON
do something like this. Does anybody know what's going on, and how I fix it?
Upvotes: 0
Views: 409
Reputation: 13513
To get the proper formatting: Use @(...)
for items you want to convert to an array in JSON.
Second, you have to use the -Depth
parameter in the ConvertTo-Json
. By default ConvertTo-Json
only goes to 2 levels deep. That is why you are getting odd results. It is returning the object type (i.e. x.ToString()
) instead of enumerating through the rest of the object as expected. So the updated code to get what you want is:
$importCommitBodyObj = [PSCustomObject]@{
delete_after_import = "0"
file_description = $null
file_title = $null
import_source = @{name="Me"; status=@{name="Shareable"}}
file_type = "6"
global_status = "1"
apply_attributes = @(@{name="Family"; value="Smith"; sources=@(@{name="Me"; status=@{name="Shareable"}})})
}
$importCommitBodyObj | ConvertTo-Json -Depth 10
Upvotes: 3