1

I've seen a lot of questions about JSON and PowerShell these past hours and none helped me find a solution to this particular problem. And I'm sure it's something easy.

I want to extract all the url fields of the plugins objects in this JSON object (original URL is this: https://updates.jenkins.io/update-center.json):

{
    "connectionCheckUrl": "http://www.google.com/",
    "core": {
        ...
    },
    "deprecations": {
        ...
    },
    "generationTimestamp": "2021-05-19T12:16:52Z",
    "id": "default",
    "plugins": {
        "42crunch-security-audit": {
            "buildDate": "Oct 06, 2020",
            "defaultBranch": "master",
            "dependencies": [
                ...
            ],
            "developers": [
                ...
            ],
            "excerpt": "Performs API contract security audit to get a detailed analysis of the possible vulnerabilities and other issues in the API contract.",
            "gav": "io.jenkins.plugins:42crunch-security-audit:3.8",
            "issueTrackers": [
                ...
            ],
            "labels": [
                ...
            ],
            ...
            "title": "42Crunch REST API Static Security Testing",
            "url": "http://archives.jenkins-ci.org/plugins/42crunch-security-audit/3.8/42crunch-security-audit.hpi",
        },
        "AnchorChain": {
            ...
            "url": "http://archives.jenkins-ci.org/plugins/AnchorChain/1.0/AnchorChain.hpi",
            ...
        },
        ... many hundreds more ...
    }
    ...
}

The plugins object contains one object per plugin, where the plugin's name is the object's key. So I somehow have to iterate over all plugin objects and look for the url property.

I want/have to do this using PowerShell (v5.1) but cannot find an easy way. Here is where I am stuck:

$all = (Get-Content(".\update-center.json") | convertfrom-json)
$all.gettype().fullname

$plugins = $all.plugins
$plugins.gettype().fullname

I get this result:

System.Management.Automation.PSCustomObject
System.Management.Automation.PSCustomObject

And now I hope to iterate over the individual plugin objects and simply get the url key's property, but I'm stuck:

$plugins | get-member -MemberType NoteProperty | foreach name | foreach $plugins.$_.url

The get-member is supposed to get the individual plugins I suppose, but hours of poring over PowerShell documentation have clearly fried my brain. Help! :-)

2 Answers 2

2

Import your JSON as you did for $all

Then, use $all.plugins | gm -MemberType Properties | select -expandproperty Name | %{ $all.plugins.$_.url} to get your list of urls

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I like that it's a one-liner, but maybe it's a bit too succinct and cryptic even though it works. :-) Some explanation would surely help to more appreciate your work.
1

I think this is what you're looking for, not exactly sure. Correct me if I'm wrong.

$Json = Invoke-RestMethod https://updates.jenkins.io/update-center.json
$Json = $Json -replace '^updateCenter.post\(|\);$' | ConvertFrom-Json

$plugins = $Json.plugins

foreach($prop in $plugins.psobject.properties.name)
{
    $plugins.$prop.url
}

Output

https://updates.jenkins.io/download/plugins/testingbot/1.16/testingbot.hpi
https://updates.jenkins.io/download/plugins/testinium/1.0/testinium.hpi
https://updates.jenkins.io/download/plugins/testlink/3.16/testlink.hpi
https://updates.jenkins.io/download/plugins/testng-plugin/1.15/testng-plugin.hpi
https://updates.jenkins.io/download/plugins/testodyssey-execution/2.1.5/testodyssey-execution.hpi
https://updates.jenkins.io/download/plugins/testopia/1.3/testopia.hpi
https://updates.jenkins.io/download/plugins/testproject/2.10/testproject.hpi
https://updates.jenkins.io/download/plugins/testquality-updater/1.3/testquality-updater.hpi
https://updates.jenkins.io/download/plugins/testsigma/1.3/testsigma.hpi
....
....
....

1 Comment

Thanks for the bonus of retrieving the actual file and removing the JSONP cruft. It is also very clear and easy to follow. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.