I have the following Json:
{
"variable1" : "variable2"
}
I won't know what the values for variable1
and variable2
are. I need to get them from the Json, presumably using two separate commands that use ConvertFrom-Json
in some way. They should be returned in plaintext format, not any PS Custom Object or anything like that.
You can export the keys and values as plain text using Out-File
.
$json=@'
{
"variable1": "variable2",
"variable3": "variable4",
"variable5": "variable6"
}
'@|ConvertFrom-Json -AsHashtable
PS /~> $json
Name Value
---- -----
variable3 variable4
variable5 variable6
variable1 variable2
PS /~> $json.Keys
variable3
variable5
variable1
PS /~> $json.Values
variable4
variable6
variable2
Edit:
Credit to @Vivere, did not know -AsHashTable
was introduced on PS Core. Supposing the test is performed on PS 5.1 here is how to get the same result. If the JSON has more depth check out the link provided by him on comment below.
PS /~> $json=@'
{
"variable1": "variable2",
"variable3": "variable4",
"variable5": "variable6"
}
'@|ConvertFrom-Json
PS /~> $json.psobject.Properties.Name
variable1
variable3
variable5
PS /~> $json.psobject.Properties.Name|%{$json.$_}
variable2
variable4
variable6