How can I remove the double quotes from PowerShell output?
The curl statement only runs when the quotes around key names are removed. I have tested my endpoint with a hard coded JSON object. (see Desired Output)
The curl statement fails with the PowerShell constructed object where the key name quotes are included. (see Actual Output)
Any hints are most appreciated.
Actual Output
{"ISOYear":[{"ISOYear":2023}],"ISOWeek":[{"ISOWeek":23}],"SkillCategory":"Warehouse","Building":"ABC"}
Desired Output
{ISOYear:[{ISOYear:2023}],ISOWeek:[{ISOWeek:29}],SkillCategory:'Warehouse',Building:'ABC'}
PowerShell Script
$hash=@{
ISOYear=@()
ISOWeek=@()
SkillCategory="Warehouse"
Building="ABC"
}
$hash.ISOYear += @{ISOYear=2023}
$hash.ISOWeek += @{ISOWeek=23}
New-Object -TypeName PSObject -Property $hash | Select-Object ISOYear,ISOWeek,SkillCategory,Building
Write-Output $hash
Pause
#THIS DOES NOT WORK WITH MY CURL STATEMENT
#KEYs include "
$json = $hash | ConvertTo-Json -Compress
Write-Output $json
#--->ADDED THIS TO ESCAPE \" $json
$jsonEscaped = $json -replace '([\\]*)"', '$1$1\"'
Write-Output $jsonEscaped
Pause
#THIS WORKS WITH MY CURL STATEMENT
#KEYs do not include "
$CURL_DATA="{ISOYear:[{ISOYear:$O_YEAR}],ISOWeek:[{ISOWeek:$O_WEEK}],SkillCategory:'Warehouse',Building:'ABC'}";
Write-Output $CURL_DATA
Pause
#WORKS
#curl.exe -k --ntlm -u: -X POST --data $CURL_DATA https://localhost:44379/api/controller/method -H "Content-Type: application/json; charset=utf-8"
#UPDATED TO RECEIVE \" escaped json
#curl.exe -k --ntlm -u: -X POST --data "$jsonEscaped" https://localhost:44379/api/controller/method -H "Content-Type: application/json; charset=utf-8"
Pause
===================UPDATE 1===================
I have added the curl statement using the PowerShell constructed object. I found a few minutes ago that I have to call it like "$json"
and it at least gets to the method. Here's the error now
This is the error I receive now.
{"errors":{"Building":["Unexpected character encountered while parsing value: B. Path 'Building', line 1, position 33."]},"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-4cbdc390e1a314d50861365e4c34aa12-f1f7defa11ef159e-00"}
===================UPDATE 2===================
Per @mklement0, his link gave a replace statement which works.
For programs that expect "-escaping, use the following -replace operation to robustly perform the extra escaping programmatically:
'...' -replace '([\]*)"', '$1$1"'
So I added this to my PowerShell script.
#--->ADDED THIS TO ESCAPE \" $json
$jsonEscaped = $json -replace '([\\]*)"', '$1$1\"'
Write-Output $jsonEscaped
I have updated the full script above.