SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-1592381%22%20slang%3D%22en-US%22%3ERe%3A%20Why%20does%20my%20generated%20JSON%20have%20too%20many%20%22%5C%5C%22%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1592381%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F743561%22%20target%3D%22_blank%22%3E%40__Martin__%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20is%20a%20way%20to%20escape%20the%20JSON%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%5Bordered%5D%40%7Bpcname%3D'ENTER%20HERE'%3Bshare%3D'%5C%5CENTER%20HERE%5CC%24'%3Bfilename%3D'ENTER%20HERE'%3Bdestfilepath%3D'some%5Cfolder'%3Bdestfile%3D'%24in.share%5C%24in.destfilepath%5C%24in.filename'%3B%0ARDdestfile%3D'C%3A%5C%24in.destfilepath%5C'%3B%0AUsername%3D%22ENTER%20HERE%22%3B%0APassword%3D%22ENTER%20HERE%22%3B%0AEncryptedPassword%3D%22%22%0A%7D%20%7C%20ConvertTo-Json%20%7C%20Foreach%20%7B%5BSystem.Text.RegularExpressions.Regex%5D%3A%3AUnescape(%24_)%7D%20%7C%20Out-File%20%22%24secFile%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThis%20will%20make%20the%20backslashes%20escape.%20Output%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%20%20%22pcname%22%3A%20%20%22ENTER%20HERE%22%2C%0A%20%20%20%20%22share%22%3A%20%20%22%5C%5CENTER%20HERE%5CC%24%22%2C%0A%20%20%20%20%22filename%22%3A%20%20%22ENTER%20HERE%22%2C%0A%20%20%20%20%22destfilepath%22%3A%20%20%22some%5Cfolder%22%2C%0A%20%20%20%20%22destfile%22%3A%20%20%22%24in.share%5C%24in.destfilepath%5C%24in.filename%22%2C%0A%20%20%20%20%22RDdestfile%22%3A%20%20%22C%3A%5C%24in.destfilepath%5C%22%2C%0A%20%20%20%20%22Username%22%3A%20%20%22ENTER%20HERE%22%2C%0A%20%20%20%20%22Password%22%3A%20%20%22ENTER%20HERE%22%2C%0A%20%20%20%20%22EncryptedPassword%22%3A%20%20%22%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1592444%22%20slang%3D%22en-US%22%3ERe%3A%20Why%20does%20my%20generated%20JSON%20have%20too%20many%20%22%5C%5C%22%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1592444%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F743561%22%20target%3D%22_blank%22%3E%40__Martin__%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%20Martin%2C%3C%2FP%3E%3CP%3EWhat%20you%20are%20doing%20is%20quit%20dangerous.%20You%20are%20breaking%20your%20JSON%20file%20and%20will%20receive%20an%20error%20when%20you%20import%20your%20JSON%20file.%20A%20backslash%20is%20reserved%20by%20the%20JSON%20and%20so%20when%20you%20want%20to%20use%20a%20backslash%20inside%20your%20data%20you%20NEED%20to%20escape%20your%20backslash%20whit%20a%20backslash.%20Doesn%E2%80%99t%20look%20to%20well%20but%20it%20has%20to%20be%20that%20way.%20The%20convertfrom-json%20cmdlet%20will%20deal%20whit%20the%20double%20backslash.%3C%2FP%3E%3CP%3EGrtz%2C%20Manfred%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1592234%22%20slang%3D%22en-US%22%3EWhy%20does%20my%20generated%20JSON%20have%20too%20many%20%22%5C%5C%22%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1592234%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Community%3C%2FP%3E%3CP%3EI%20have%20a%20Powershell%20Script%20which%20generates%20a%20JSON%20file%20with%20data%20in%20it.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20problem%20with%20this%20file%20though.%20It%20generates%20double%20the%20amount%20of%20%22%5C%5C%22!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20know%20how%20I%20could%20solve%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20my%20Code%20to%20generate%20the%20JSON%20File%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%5Bordered%5D%40%7B%0Apcname%3D'ENTER%20HERE'%3B%0Ashare%3D'%5C%5CENTER%20HERE%5CC%24'%3B%0Afilename%3D'ENTER%20HERE'%3B%0Adestfilepath%3D'some%5Cfolder'%3B%0Adestfile%3D'%24in.share%5C%24in.destfilepath%5C%24in.filename'%3B%0ARDdestfile%3D'C%3A%5C%24in.destfilepath%5C'%3B%0AUsername%3D%22ENTER%20HERE%22%3B%0APassword%3D%22ENTER%20HERE%22%3B%0AEncryptedPassword%3D%22%22%0A%7D%20%7C%20ConvertTo-Json%20%7C%20Out-File%20%22%24secFile%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24secFile%20is%20just%20a%20path%20to%20save%20the%20file%20to.%20Just%20tell%20me%20if%20you%20need%20this%20too.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20output%20JSON%20file%20looks%20liek%20this%20though%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%22pcname%22%3A%20%22ENTER%20HERE%22%2C%0A%22share%22%3A%20%22%5C%5C%5C%5CENTER%20HERE%5C%5CC%24%22%2C%0A%22filename%22%3A%20%22ENTER%20HERE%22%2C%0A%22destfilepath%22%3A%20%22some%5C%5Cfolder%22%2C%0A%22destfile%22%3A%20%22%24in.share%5C%5C%24in.destfilepath%5C%5C%24in.filename%22%2C%0A%22RDdestfile%22%3A%20%22C%3A%5C%5C%24in.destfilepath%5C%5C%22%2C%0A%22Username%22%3A%20%22ENTER%20HERE%22%2C%0A%22Password%22%3A%20%22ENTER%20HERE%22%2C%0A%22EncryptedPassword%22%3A%20%22%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreetings%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMartin%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEdit%3A%20I%20also%20posted%20this%20question%20in%20the%20PowerShell.org%20Forum%20and%20Stackoverflow%2C%20just%20so%20you%20know%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fpowershell.org%2Fforums%2Ftopic%2Fwhy-does-my-generated-json-have-too-many%2F%22%20rel%3D%22nofollow%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fpowershell.org%2Fforums%2Ftopic%2Fwhy-does-my-generated-json-have-too-many%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F63446524%2Fwhy-does-my-generated-json-have-too-many%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F63446524%2Fwhy-does-my-generated-json-have-too-many%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1592234%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECommunity%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional 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
Highlighted

@__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":  ""
}
Highlighted
Best Response confirmed by __Martin__ (Occasional 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