Get value in JSON object from external URI

Viewed 1105

In my PowerShell code I have:

$retrieveSourceBranchFromBuildURL = "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId" + "?api-version=5.0"

When I navigate to the URL in the browser I see:

{
    _links: {
    self: {
    href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46070"
    },
    web: {
    href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_build/results?buildId=46070"
    },
    sourceVersionDisplayUri: {
    href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/sources"
    },
    timeline: {
    href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/Timeline"
    },
    badge: {
    href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/status/67"
    }
    },
    parameters: "{"system.pullRequest.pullRequestId":"5766","system.pullRequest.sourceBranch":"refs/heads/pb/31333-test-branch/name","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"1cf19b95a59478a8554c2c03d65dcefe203529a6","system.pullRequest.sourceRepositoryUri":"https://something@dev.azure.com/something/something%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"1"}",
}  

The $buildInformation variable:

$buildInformation = Invoke-RestMethod -Uri $retrieveSourceBranchFromBuildURL -Headers @{Authorization = $pat } -Method Get -ContentType 'application/json'

When I output the value of $buildInformation I get:

 @{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=46079; buildNumber=20190624.3; status=completed; result=succeeded; queueTime=2019-06-24T07:57:05.7271255Z; startTime=2019-06-24T07:57:12.8021227Z; finishTime=2019-06-24T08:12:27.003113Z; url=https://dev.azure.com/embrace/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46079; definition=; buildNumberRevision=3; project=; uri=vstfs:///Build/Build/46079; sourceBranch=refs/pull/5740/merge; sourceVersion=735f7a813c343dbbca5e1d1b3e966e0bad1db762; priority=normal; reason=pullRequest; requestedFor=; requestedBy=; lastChangedDate=2019-06-24T08:14:41.11Z; lastChangedBy=; parameters={"system.pullRequest.pullRequestId":"5740","system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276","system.pullRequest.sourceRepositoryUri":"https://embrace@dev.azure.com/embrace/Embrace%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"5"}; orchestrationPlan=; logs=; repository=; keepForever=False; retainedByRelease=True; 

When I output the value of $buildInformation.parameters:

Write-Host $buildInformation.parameters

{
    "system.pullRequest.pullRequestId":"5740",
    "system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team",
    "system.pullRequest.targetBranch":"refs/heads/master",
    "system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276",
    "system.pullRequest.sourceRepositoryUri":"https://embrace@dev.azure.com/embrace/Embrace%20Suite/_git/client-web",
    "system.pullRequest.pullRequestIteration":"5"
}

But when I try to read a value:

Write-Host $buildInformation.parameters.system.pullRequest.sourceBranch

The output is empty.

So how do I retrieve the parameters.system.pullRequest.sourceBranch value?

1 Answers

To access the property with 'special characters' in its name, enclose the parameter name inside quotes like so:

$buildInformation.parameters.'system.pullRequest.sourceBranch'

NOTE: The trick here is that the value of parameters is inside double quotes. Therefore you might not be able to access the properties under parameters the usual way. To workaround it, you can make a conversion directly on parameters:

$convertedParams = $buildInformation.parameters | ConvertFrom-Json

# Access desired property
$convertedParams.'system.pullRequest.sourceBranch'

When you access the property like you tried

$buildInformation.parameters.system.pullRequest.sourceBranch

the structure of your JSON should be like this:

PS> $buildInformation = '{
"parameters": {
  "system": {
    "pullRequest":{
      "sourceBranch": "refs/heads/master-md/social-create-team"
      }
    }
  }
}' | ConvertFrom-Json

# Checking output
PS> $buildInformation.parameters.system.pullRequest.sourceBranch
refs/heads/master-md/social-create-team
Related