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

Thursday, July 14, 2022

[FIXED] How can I convert a parsed json file from json then back to json?

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

Issue

I am generating a parsed JSON file using powershell ConvertTo-Json. Everything is fine until here, because now I want to convert it from json, use the values and then convert it back to json with the values like they were before. But when I convert the file back to json, it only shows null for the values... Is there any way to solve this?

Here is my code for creating the parsed file:

$secFile = "C:\some\folder\creds.json"
$in = Get-Content $secFile | ConvertFrom-Json
[ordered]@{
        pcname='ENTER HERE';
        share='\\$in.pcname\C$';
        filename='ENTER HERE';
        destfilepath='Scripts\Cert';
        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"

Here is the code for converting the file back to json:

[ordered]@{
            pcname=$in.pcname;
            share=$in.share;
            filename=$in.filename;
            destfilepath=$in.destfilepath;
            destfile=$in.destfile;
            RDdestfile=$in.RDdestfile;
            Username=$in.Username;
            Password="";
            EncryptedPassword="$secString"
        } | ConvertTo-Json | Out-File "$secFile"

and here is the file after converting it back to json:

{
    "pcname":  null,
    "share":  null,
    "filename":  null,
    "destfilepath":  null,
    "destfile":  null,
    "RDdestfile":  null,
    "Username":  null,
    "Password":  "",
    "EncryptedPassword":  "01000000d08c9ddf0115d1118c7a00c04fc297eb010000006f6d3ce161a681428efe68b51827a6640000000002000000000003660000c0000000100000002a6a1ff60cb280662a9578cb47926a4d0000000004800000a000000010000000a65a1cd7137935dbfcd22bcdc685f52a20000000b87554b4f6f6dbe655cd525a894e1c7d1180b4db121385e57b218fa772ad1d441400000048453bb6e137ed437de3e4ecbd855429ddfc1fba"
}

This worked before I parsed the file. So that can't be the error, right?

I'm neither a powershell or json pro, so I really am hoping for good help

Greetings

Martin


Solution

If I take your example and export and reimport, I get an error.

[pscustomobject]@{

    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 -OutVariable results |
    Foreach {[System.Text.RegularExpressions.Regex]::Unescape($_)} |
        Out-File $secFile

Get-Content $secFile | Convertfrom-json

Error

At line:14 char:28
+     Get-Content $secFile | Convertfrom-json
+                            ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

However if I simply remove the regex unescaping, it works fine.

[pscustomobject]@{

    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 -OutVariable results| Out-File $secFile

Get-Content $secFile | Convertfrom-json

Output

pcname            : ENTER HERE
share             : \\ENTER HERE\C$
filename          : ENTER HERE
destfilepath      : some\folder
RDdestfile        : C:\$in.destfilepath\
Username          : ENTER HERE
Password          : ENTER HERE
EncryptedPassword : 

Is that required?



Answered By - Doug Maurer
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • 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