Home > Software engineering >  How to read specific Key from JSON file with Powershell
How to read specific Key from JSON file with Powershell

Time:03-17

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.

CodePudding user response:

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

  • Related