managed to get the JSON data with
$R= Invoke-WebRequest -Uri $url -Headers $headers -ContentType "application/json" -Method Get -UseBasicParsing
$x = $R.Content | Out-String | ConvertFrom-Json
now I've a JSON file like this :
{
"id": 1000,
"name_with_namespace": "myProjectName1",
},
{
"id": 1001,
"name_with_namespace": "myProjectName2",
},
{
"id": 1002,
"name_with_namespace": "myProjectName3",
}
now I need to extract all the occurence of "name_with_namespace"
and "id"
where "name_with_namespace" = "myProjectName.*"
How can I do it in one shot?
I've tried this:
foreach ($name in $x.name_with_namespace) {
if ( $name -match ".*myProjectName.*" ) {
write-host "$name - $x.id"
}
}
but I got this:
myProjectName1 1000 1001 1002
myProjectName2 1000 1001 1002
myProjectName3 1000 1001 1002
I understand that the issue is on $x.id, how can I get the right value instead of everything ?
my expected output is
myProjectName1 1000
myProjectName2 1001
myProjectName3 1002
One way to query an API with PowerShell and get some JSON in return is to use the Invoke-WebRequest cmdlet. This cmdlet can query any web service/site over HTTP and return information (not just JSON). In this example, you're using it as an example to get some JSON to work with.
First, we need to load the JSON from our file. To do this we use the Get-Content cmdlet and tell it that we want to load the JSON from our file created above. After that we use the pipe operator | to, well, pipe the result into our next cmdlet which is called ConvertFrom-Json .
To convert the JSON file to the CSV file using PowerShell, we need to use the ConvertTo-CSV command as a pipeline. For example, we have a JSON file called PatchingServer. JSON stored at C:\temp and its content is as below.
You're drilling down a level too far when you define your loop. Just iterate $x
and then check against the specific property inside the loop.
foreach ($name in $x) {
if ( $name.name_with_namespace -match ".*myProjectName.*" ) {
write-host "$($name.name_with_namespace) - $($name.id)"
}
}
Edit: On a side note, you can simplify your -match
to just "myProjectName"
and it will work just as well, and probably faster.
ConvertFrom-Json
has some odd behavior with pipelines. The issue is that ConvertFrom-Json
wraps the JSON array in an array and then passes the whole array down the pipeline as one item. This is fine in most cases, but if the outermost level is a JSON array, then that whole array gets passed as a single object into the pipeline.
Compare:
PS> ConvertFrom-Json '[1, 2, 3]' | ForEach-Object {": $_"}
: 1 2 3
PS> (ConvertFrom-Json '[1, 2, 3]') | ForEach-Object {": $_"}
: 1
: 2
: 3
PS> $x = ConvertFrom-Json '[1, 2, 3]'
PS> $x | ForEach-Object {": $_"}
: 1
: 2
: 3
PS> ,$x | ForEach-Object {": $_"}
: 1 2 3
Note with that last example we can duplicate the problem with the unary comma operator.
The issue has been reported here for PowerShell Core 6.
Try this code:
$j = @'
[
{
"id": 1000,
"name_with_namespace": "myProjectName1"
},
{
"id": 1001,
"name_with_namespace": "myProjectName2"
},
{
"id": 1002,
"name_with_namespace": "myProjectName3"
}
]
'@
($j | Out-String | ConvertFrom-Json) |
Where-Object name_with_namespace -match ".*myProjectName.*" |
ForEach-Object {
"$($_.name_with_namespace) $($_.id)"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With