0

I'm getting the following response through an API and I'm trying to pull data out of the JSON response. I'm interested in only pulling the clone.href when clone.name = ssh.

response: {
    "links":  {
        "clone":  [
            "@{href=ssh://sampleurl.com; name=ssh}",
            "@{href=https://sampleurl.com; name=http}"
        ],
        "self":  [
            "@{href=https://sampleurl.com}"
        ]
    }
}

I'm using the following to call the API:

Invoke-RestMethod -Uri $uri -Headers $Header -Method POST -Body $Body|ConvertTo-Json
1
  • Is this how the Json actually looks? Try using ConvertTo-Json -Depth 30 and see if the Json looks like the one you have posted here Commented Jun 1, 2022 at 22:13

1 Answer 1

1

You can do this:

$result = Invoke-RestMethod -Uri $uri -Headers $Header -Method POST -Body $Body|ConvertTo-Json

$href = $result.links.clone | Where-Object Name -eq ssh | ForEach-Object href
$href # Output to console

This uses Where-Object to filter the clone array and ForEach-Object to extract the href property, using short form of ForEach-Object -MemberName href.

Alternatively you can use the following syntax:

$href = $result.links.clone.Where{ $_.Name -eq 'ssh' }.href

It uses PowerShell intrinsic method Where for filtering.

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.