Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read specific Key from JSON file with Powershell

I’m trying to read a Json file with some passwords but them are stored into Key-Value pattern as below:

{
    "id":  "a8cc4184-3844-4605-8e35-ea479ae1094e",
    "name":  "Test Credentials and Secrets",
    "values":  [
                   {
                       "key":  "apassword",
                       "value":  "mypassword",
                       "enabled":  true
                   },
                   {
                       "key":  "tpassword",
                       "value":  "my other password",
                       "enabled":  true
                   },
               ],
    "exported_using":  "Postman/7.34.0"
}

How can I get any specific value with PowerShell? tpassword value for instance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would recommend using ConvertFrom-Json to turn it into an Object so you can then access the properties easily with PowerShell.

I saved the json above to C:\temp\json.json and then the following got me the tpassword value:

$json = Get-Content C:\temp\json.json | ConvertFrom-Json
$json.values | Where-Object key -eq 'tpassword' | Select-Object Value

PowerShell terminal showing results

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading