PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 14, 2022

[FIXED] Why does my generated JSON have too many "\\"?

 July 14, 2022     json, powershell, web-deployment, windows     No comments   

Issue

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 the Microsoft Tech Community, just so you know

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

https://techcommunity.microsoft.com/t5/windows-powershell/why-does-my-generated-json-have-too-many-quot-quot/td-p/1592234


Solution

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


Answered By - Wasif
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing