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

%3CLINGO-SUB%20id%3D%22lingo-sub-1592423%22%20slang%3D%22en-US%22%3EHow%20can%20I%20convert%20a%20parsed%20json%20file%20from%20json%20then%20back%20to%20json%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1592423%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Community%3C%2FP%3E%3CP%3EI%20am%20generating%20a%20parsed%20JSON%20file%20using%20powershell%20ConvertTo-Json.%3CBR%20%2F%3EEverything%20is%20fine%20until%20here%2C%20because%20now%20I%20want%20to%20convert%20it%20from%20json%2C%20use%20the%20values%20and%20then%20convert%20it%20back%20to%20json%20with%20the%20values%20like%20they%20were%20before.%3CBR%20%2F%3EBut%20when%20I%20convert%20the%20file%20back%20to%20json%2C%20it%20only%20shows%20null%20for%20the%20values...%3CBR%20%2F%3EIs%20there%20any%20way%20to%20solve%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20my%20code%20for%20creating%20the%20parsed%20file%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24secFile%20%3D%20%22C%3A%5Csome%5Cfolder%5Ccreds.json%22%0A%24in%20%3D%20Get-Content%20%24secFile%20%7C%20ConvertFrom-Json%0A%5Bordered%5D%40%7B%0Apcname%3D'ENTER%20HERE'%3B%0Ashare%3D'%5C%5C%24in.pcname%5CC%24'%3B%0Afilename%3D'ENTER%20HERE'%3B%0Adestfilepath%3D'Scripts%5CCert'%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%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%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20code%20for%20converting%20the%20file%20back%20to%20json%3A%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%24in.pcname%3B%0Ashare%3D%24in.share%3B%0Afilename%3D%24in.filename%3B%0Adestfilepath%3D%24in.destfilepath%3B%0Adestfile%3D%24in.destfile%3B%0ARDdestfile%3D%24in.RDdestfile%3B%0AUsername%3D%24in.Username%3B%0APassword%3D%22%22%3B%0AEncryptedPassword%3D%22%24secString%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%3Eand%20here%20is%20the%20file%20after%20converting%20it%20back%20to%20json%3A%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%20null%2C%0A%22share%22%3A%20null%2C%0A%22filename%22%3A%20null%2C%0A%22destfilepath%22%3A%20null%2C%0A%22destfile%22%3A%20null%2C%0A%22RDdestfile%22%3A%20null%2C%0A%22Username%22%3A%20null%2C%0A%22Password%22%3A%20%22%22%2C%0A%22EncryptedPassword%22%3A%20%2201000000d08c9ddf0115d1118c7a00c04fc297eb010000006f6d3ce161a681428efe68b51827a6640000000002000000000003660000c0000000100000002a6a1ff60cb280662a9578cb47926a4d0000000004800000a000000010000000a65a1cd7137935dbfcd22bcdc685f52a20000000b87554b4f6f6dbe655cd525a894e1c7d1180b4db121385e57b218fa772ad1d441400000048453bb6e137ed437de3e4ecbd855429ddfc1fba%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EThis%20worked%20before%20I%20parsed%20the%20file.%20So%20that%20can't%20be%20the%20error%2C%20right%3F%3C%2FP%3E%3CP%3EI'm%20neither%20a%20powershell%20or%20json%20pro%2C%20so%20I%20really%20am%20hoping%20for%20good%20help%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%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1592423%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECommunity%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1592465%22%20slang%3D%22en-US%22%3ERe%3A%20How%20can%20I%20convert%20a%20parsed%20json%20file%20from%20json%20then%20back%20to%20json%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1592465%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%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%20Martin%2C%3C%2FP%3E%3CP%3EPlease%20check%20my%20response%20at%20your%20earlier%20post%3A%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-powershell%2Fwhy-does-my-generated-json-have-too-many-quot-quot%2Fm-p%2F1592234%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fwindows-powershell%2Fwhy-does-my-generated-json-have-too-many-quot-quot%2Fm-p%2F1592234%3C%2FA%3E%3C%2FP%3E%3CP%3EThis%20is%20the%20exact%20reason%20why!%3C%2FP%3E%3CP%3EGrt%2C%20Manfred%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Dear Community

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

1 Reply
Highlighted

@__Martin__ 

 

Hello Martin,

Please check my response at your earlier post: https://techcommunity.microsoft.com/t5/windows-powershell/why-does-my-generated-json-have-too-many-q...

This is the exact reason why!

Grt, Manfred