0

I'm not sure how to name these elements properly, it'll be easier just to show it. I have following JSON:

{
  "DEV": [
    {
      "GitEmail": "[email protected]"
    }
  ],
  "TEST": [
    {
      "GitEmail": "[email protected]"
    }
  ],
  "PROD": [
    {
      "GitEmail": "[email protected]"
    }
  ]
}  

I would like to get the "DEV" by providing it's email. How to implement that in powershell?

3 Answers 3

2

Something like below can help -

PS> $json = '{
  "DEV": [
    {
      "GitEmail": "[email protected]"
    }
  ],
  "TEST": [
    {
      "GitEmail": "[email protected]"
    }
  ],
  "PROD": [
    {
      "GitEmail": "[email protected]"
    }
  ]
}' | ConvertFrom-Json

PS> ($json.psobject.Properties | ? {$_.Value -match "[email protected]"}).Name

Depending on the email matches you can retrieve the environment names.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi - can You help me on how did You know to check $json.psobject.Properties If You do $json|get-member - it does not show that $json object has psobject property or method. What kind of property is it?
After the JSON has been converted, if you look at it's type, $json.GetType(), you will notice that it is a PSCustomObject deriving from the System.Object base class. All the PS custom objects will have the PSObject.Properties Property since it again derives from the System.Management.Automation namespace. See the corresponding msdn link for more details.
1

I can't promise there is an easier method, but this here is one way:

Given that you json is stored in a variable $json:

You can get every head object with $json.psobject.properties.name:

Input:

$json.psobject.properties.name

Output:

DEV
TEST
PROD

With this we can create a foreach loop and search for the Email:

foreach ($dev in $json.psobject.properties.name)
{
  if($json.$dev.GitEmail -eq "[email protected]") {
    echo $dev
  }
}

Comments

1

I do not know any elegant way of doing it. ConvertFrom-Json does not create neat objects with easy ways to traverse them like convertfrom-xml, instead result is just a PsObject with bunch of noteproperties.
What I do in such cases is

    $a= @"
{
  "DEV": [
    {
      "GitEmail": "[email protected]"
    }
  ],
  "TEST": [
    {
      "GitEmail": "[email protected]"
    }
  ],
  "PROD": [
    {
      "GitEmail": "[email protected]"
    }
  ]
}  
"@
$JsonObject= ConvertFrom-Json -InputObject $a 
$NAMES= $JsonObject|Get-Member |WHERE MemberType -EQ NOTEPROPERTY
$NAMES|Foreach-Object {IF($JsonObject.$($_.NAME).GITEMAIL -EQ '[email protected]'){$_.NAME}}

Result of above is

DEV

Not pretty, not really re-usable but works. If anyone knows a better way of going about it - I'll be happy to learn it:)

2 Comments

You have missing } in the code. However I choose another as a winner as it is single line solution
Thanks . have added the missing } to my answer. I agree the answer You choose is better. I'll be using that since now too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.