Reputation: 55
I have a json output from an api call, which partially looks like as below. As part of this json, there are many id and name values listed..Now, I want to capture id and name pair together as an array , as part of some steps in the program I get returned with name , I want to compare this name with the name stored in array previously and return the id value corresponding to it. As I have to pass this id value to another rest api call.
Could someone help how can I capture this array through powershell?
$Json = '{
"value": [
{
"id": "1",
"name": "datafactory.git",
"size": 0,
"remoteUrl": "",
"sshUrl": "",
"webUrl": "",
"isDisabled": false
},
{
"id": "2",
"name": "datafactory",
"defaultBranch": "refs/heads/main",
"size": 13569,
"remoteUrl": "",
"sshUrl": "",
"webUrl": "",
"isDisabled": false
},
]
}'
Upvotes: 1
Views: 373
Reputation: 23623
I would create a hash table for this:
($Json |ConvertFrom-Json).Value |ForEach-Object {
$HashTable = @{}
} {
$HashTable[$_.name] = $_.id
}
Usage:
$HashTable.datafactory
2
You might also consider to refer to the complete value property object (instead of just the id
):
($Json |ConvertFrom-Json).Value |ForEach-Object {
$HashTable = @{}
} {
$HashTable[$_.name] = $_
}
Usage:
$HashTable.datafactory.id
2
$HashTable.datafactory.size
13569
Upvotes: 1