SOLVED

Why does my generated JSON have too many "\\"?

Copper Contributor

Dear Community

I have a Powershell Script which generates a JSON file with data in it. 

I have a problem with this file though. It generates double the amount of "\\"!

 

Do you know how I could solve this?

 

Here is my Code to generate the JSON File:

 

 

 

[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 | Out-File "$secFile"

 

 

 

$secFile is just a path to save the file to. Just tell me if you need this too.

 

The output JSON file looks liek this though:

 

 

{
"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": ""
}

 

 

Greetings

 

Martin

 

Edit: I also posted this question in the PowerShell.org Forum and Stackoverflow, just so you know

https://powershell.org/forums/topic/why-does-my-generated-json-have-too-many/

https://stackoverflow.com/questions/63446524/why-does-my-generated-json-have-too-many

2 Replies

@__Martin__ 

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":  ""
}
best response confirmed by __Martin__ (Copper Contributor)
Solution

@__Martin__ 

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

1 best response

Accepted Solutions
best response confirmed by __Martin__ (Copper Contributor)
Solution

@__Martin__ 

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

View solution in original post