0

I am trying to use PowerShell to extract value from JSON object, I have the following JSON:

{
  "aad.NTA5YjRjYTItNDFjNi03OTQ1LTlmNTYtM2MyOGI1NWE5MTU5": {
    "descriptor": "aad.NTA5YjRjYTItNDFjNi03OTQ1LTlmNTYtM2MyOGI1NWE5MTU5",
    "directoryAlias": "dis3yv",
    "displayName": "Tom Test",
    "domain": "ea80952e-a476-42d4-aaf4-5457awesfre",
    "legacyDescriptor": null,
    "mailAddress": "[email protected]",
    "metaType": "member",
    "origin": "aad",
    "originId": "919579e1-6e98-47fd-adb3-3d52d0467037",
    "principalName": "[email protected]"
  },
  "aad.NmE5ODg4ODQtM2EyNS03ZjBiLWI0OTItN2JmMDA1MzVkNmJi": {
    "descriptor": "aad.NmE5ODg4ODQtM2EyNS03ZjBiLWI0OTItN2JmMDA1MzVkNmJi",
    "directoryAlias": "cup7mz",
    "displayName": "Dummy May",
    "domain": "ea80952e-a476-42d4-aaf4-5457852b0f7e",
    "legacyDescriptor": null,
    "mailAddress": "[email protected]",
    "metaType": "member",
    "origin": "aad",
    "originId": "71e2fa85-59f0-4d8f-a5a9-4ff3e23d01d9",
    "principalName": "[email protected]",
    "subjectKind": "user"
  }
}

Here I want to extract mailaddress from this file using powershell, and assign it to variable.

The problem I have is the top value "aad.*" keeps changing and I want to be able to automat this so I can have all the mailaddresses in an array or something

so i can't use

$yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).aad.NTA5YjRjYTItNDFjNi03OTQ1LTlmNTYtM2MyOGI1NWE5MTU5.mailaddress

because the value keeps changing

Anyone know how to do this ?

1 Answer 1

2

What you will need to do is foreach by the PSObject.Properties

For simplicity, I assigned the json to a variable in a the script as an example. You can still call it from a file location:

$jsonFile = @'
{
  "aad.NTA5YjRjYTItNDFjNi03OTQ1LTlmNTYtM2MyOGI1NWE5MTU5": {
    "descriptor": "aad.NTA5YjRjYTItNDFjNi03OTQ1LTlmNTYtM2MyOGI1NWE5MTU5",
    "directoryAlias": "dis3yv",
    "displayName": "Tom Test",
    "domain": "ea80952e-a476-42d4-aaf4-5457awesfre",
    "legacyDescriptor": null,
    "mailAddress": "[email protected]",
    "metaType": "member",
    "origin": "aad",
    "originId": "919579e1-6e98-47fd-adb3-3d52d0467037",
    "principalName": "[email protected]"
  },
  "aad.NmE5ODg4ODQtM2EyNS03ZjBiLWI0OTItN2JmMDA1MzVkNmJi": {
    "descriptor": "aad.NmE5ODg4ODQtM2EyNS03ZjBiLWI0OTItN2JmMDA1MzVkNmJi",
    "directoryAlias": "cup7mz",
    "displayName": "Dummy May",
    "domain": "ea80952e-a476-42d4-aaf4-5457852b0f7e",
    "legacyDescriptor": null,
    "mailAddress": "[email protected]",
    "metaType": "member",
    "origin": "aad",
    "originId": "71e2fa85-59f0-4d8f-a5a9-4ff3e23d01d9",
    "principalName": "[email protected]",
    "subjectKind": "user"
  }
}
'@

Convert and foreach by PSObject.Propteries:

#convert
$jsonObject = ($jsonFile | ConvertFrom-Json)
#display all Mailaddress
$jsonObject.PSObject.Properties | %{$_.Value.MailAddress}

In your example it should just be:

$yourVariable.PSObject.Properties | %{$_.Value.MailAddress}

If you don't want to foreach and you can call it directly like so:

$yourVariable.PSObject.Properties.Value.MailAddress

The foreach allows you to call more than one property value at a time though.

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

Comments

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.