I have looked at many examples, but I cannot get my code to work. The structure of this JSON seems to be different than any others I've seen. I need to iterate through these results and get "id" and "legalName".
[
"URL: https://myurl.com/rest/api",
{
"value": [
{
"id": 271,
"client": "@{id=245; clientCode=; clientName=}",
"dbaName": null,
"federalEIN": "",
"legalCode": "01",
"legalName": "Test1",
"naicsCode": null,
"status": "Active",
"links": " "
},
{
"id": 272,
"client": "@{id=245; clientCode=; clientName=}",
"dbaName": null,
"federalEIN": "",
"legalCode": "02",
"legalName": "Test2",
"naicsCode": null,
"status": "Terminated",
"links": " "
}
],
"Count": 6
}
]
I've tried lots of different code examples, but none of them have worked. This returns nothing. I can't seem to get down to the properties I need.
$var = Get-RestData("https://myurl.com/rest/api/") | ConvertTo-Json
$var["value"] | select id
You can use the ConvertFrom-Json
cmdlet to convert from a JSON formatted string to a PSCustomObject
with which you can iterate over.
$json = @"
[
"URL: https://myurl.com/rest/api",
{
"value": [
{
"id": 271,
"client": "@{id=245; clientCode=; clientName=}",
"dbaName": null,
"federalEIN": "",
"legalCode": "01",
"legalName": "Test1",
"naicsCode": null,
"status": "Active",
"links": " "
},
{
"id": 272,
"client": "@{id=245; clientCode=; clientName=}",
"dbaName": null,
"federalEIN": "",
"legalCode": "02",
"legalName": "Test2",
"naicsCode": null,
"status": "Terminated",
"links": " "
}
],
"Count": 6
}
]
"@
$json | ConvertFrom-Json | ForEach-Object { $_.value } | Select id, legalName
which returns:
id legalName
-- ---------
271 Test1
272 Test2