I have without success tried to figure out how to most efficiently fetch data with powershell from the below JSON that is returned to me by a REST API:
{
"$schema": "api:standardResponse",
"links": [
{
"rel": "canonical",
"href": "http://localhost:8501/services/v2/replicats",
"mediaType": "application/json"
},
{
"rel": "self",
"href": "http://localhost:8501/services/v2/replicats",
"mediaType": "application/json"
},
{
"rel": "describedby",
"href": "http://localhost:8501/services/v2/metadata-catalog/replicats",
"mediaType": "application/schema+json"
}
],
"messages": [],
"response": {
"$schema": "ogg:collection",
"items": [
{
"links": [
{
"rel": "parent",
"href": "http://localhost:8501/services/v2/replicats",
"mediaType": "application/json"
},
{
"rel": "canonical",
"href": "http://localhost:8501/services/v2/replicats/RNIMDA00",
"mediaType": "application/json"
}
],
"$schema": "ogg:collectionItem",
"name": "RNIMDA00"
},
{
"links": [
{
"rel": "parent",
"href": "http://localhost:8501/services/v2/replicats",
"mediaType": "application/json"
},
{
"rel": "canonical",
"href": "http://localhost:8501/services/v2/replicats/RNIMDA01",
"mediaType": "application/json"
}
],
"$schema": "ogg:collectionItem",
"name": "RNIMDA01"
},
{
"links": [
{
"rel": "parent",
"href": "http://localhost:8501/services/v2/replicats",
"mediaType": "application/json"
},
{
"rel": "canonical",
"href": "http://localhost:8501/services/v2/replicats/RNIMDA02",
"mediaType": "application/json"
}
],
"$schema": "ogg:collectionItem",
"name": "RNIMDA02"
}
]
}
}
I only need the data from the "name": node, and the data from the "href": node.
I have done some searching and found som examples where the JSON is converted with ConvertFrom-Json and then iterated with a foreach like the pseudo code below:
$users = $response | ConvertFrom-Json
foreach ($user in $users)
{
write-host "$($user.name) has the email: $($user.email)"
}
But I wonder if there is a better way of fetching data with powershell from a object that contains JSON.
Thanks :)
ConvertFrom-Jsonsolution you already found?$Users.response.items.name