Forum Discussion
Why does my generated JSON have too many "\\"?
- Aug 17, 2020
Hello Martin,
What you are doing is quit dangerous. You are breaking your JSON file and will receive an error when you import your JSON file. A backslash is reserved by the JSON and so when you want to use a backslash inside your data you NEED to escape your backslash whit a backslash. Doesn’t look to well but it has to be that way. The convertfrom-json cmdlet will deal whit the double backslash.
Grtz, Manfred
There is a way to escape the JSON:
[ordered]@{pcname='ENTER HERE';share='\\ENTER HERE\C$';filename='ENTER HERE';destfilepath='some\folder';destfile='$in.share\$in.destfilepath\$in.filename';
RDdestfile='C:\$in.destfilepath\';
Username="ENTER HERE";
Password="ENTER HERE";
EncryptedPassword=""
} | ConvertTo-Json | Foreach {[System.Text.RegularExpressions.Regex]::Unescape($_)} | Out-File "$secFile"
This will make the backslashes escape. Output:
{
"pcname": "ENTER HERE",
"share": "\\ENTER HERE\C$",
"filename": "ENTER HERE",
"destfilepath": "some\folder",
"destfile": "$in.share\$in.destfilepath\$in.filename",
"RDdestfile": "C:\$in.destfilepath\",
"Username": "ENTER HERE",
"Password": "ENTER HERE",
"EncryptedPassword": ""
}
Hello Martin,
What you are doing is quit dangerous. You are breaking your JSON file and will receive an error when you import your JSON file. A backslash is reserved by the JSON and so when you want to use a backslash inside your data you NEED to escape your backslash whit a backslash. Doesn’t look to well but it has to be that way. The convertfrom-json cmdlet will deal whit the double backslash.
Grtz, Manfred