10

I have the following json output string:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
    },
    "objects": [{
        "bcontext": "/api/v2.0/buildercontext/2/",
        "bugs": [],
        "build": {
            "bldtype": "obj",
            "branch": "main",
            "buildstatus": [{
                "build": "/api/v2.0/build/2140634/",
                "failurereason": "_checkfailures (seen: FAIL - /testrun/18647678/ - area[4769] AIM-SANITY)",
                "id": "1294397",
                "lastupdate": "2015-03-31T14:30:18",
                "overridden": false,
                "overridedesc": "",
                "overrideuser": null,
                "recommended": false,
                "resource_uri": "/api/v2.0/buildstatus/1294397/",
                "slatype": {
                    "id": "26",
                    "name": "VA_Bats",
                    "resource_uri": "/api/v2.0/sla/26/"
                }
            }],
            "changeset": "494625",
            "coverage": false,
            "deliverables": ["/api/v2.0/deliverable/4296455/", "/api/v2.0/deliverable/4296956/", "/api/v2.0/deliverable/4296959/", "/api/v2.0/deliverable/4296986/", "/api/v2.0/deliverable/4296992/", "/api/v2.0/deliverable/4296995/", "/api/v2.0/deliverable/4297034/", "/api/v2.0/deliverable/4297058/"],
            "git_host": null,
            "git_repo": null,
            "id": "2140634",
            "p4host": {
                "id": "10",
                "p4port": "perforce-rhino.eng.com:1800",
                "p4weburl": "http://p4web.eng.com:1800",
                "resource_uri": "/api/v2.0/perforceserver/10/"
            },
            "resource_uri": "/api/v2.0/build/2140634/",
            "site": "/api/v2.0/site/25/",
            "site_name": "mbu",
            "slastested": ["/api/v2.0/sla/26/"],
            "submit_time": "2015-03-31T05:40:21",
            "submit_user": "haharonof"
        },
        "builder": "/api/v2.0/builder/1423/",
        "clean": true,
        "componentbuilds": "vcops-vsphere-solution-pak=sb-5242047,vrops=sb-5242013,vscm=sb-5242025,vsutilities=sb-5242029;parentbuilder=1410",
        "deleted": false,
        "endtime": "2015-03-31T06:20:58",
        "helpzillas": [],
        "id": "4296956",
        "location": {
            "httpserver": "sc-prd-cat-services001.eng.com",
            "id": "1",
            "name": "PA",
            "nfsserver": "cat-results.eng.com",
            "pxedir": "/mts/builder-pxe",
            "resource_uri": "/api/v2.0/location/1/",
            "resultspath": "/results"
        },
        "nfsserver": "build-storage60",
        "p4client": "vmktestdevnanny-builder-1423",
        "path": "/storage60/release/sb-5242148",
        "ready": true,
        "resource_uri": "/api/v2.0/deliverable/4296956/",
        "result": "PASS",
        "sbbuildid": 5242148,
        "sbjobid": 5242148,
        "sbuser": "arajamanickam",
        "starttime": "2015-03-31T06:16:50",
        "targetchangeset": "494625",
        "targets": "vcopssuitevm",
        "triagetime": null,
        "vmodl": null
    }]
}

I want to get sbbuildid using powershell. How can I get this?

2 Answers 2

20

By converting your json to an object, using the ConvertFrom-Json cmdlet (assuming $jsonString contains the json above):

$jsonObj = $jsonString | ConvertFrom-Json
$jsonObj.objects.sbbuildid
Sign up to request clarification or add additional context in comments.

2 Comments

Just a nitpick. This is not the key value. In practice this actually appears to get the entire key, not just the value. Write-Host, for example, will only show you the value. But if you do anything with the object you will find it has the key name and value.
@SixOThree I'm afraid I don't understand what you're trying to say here. Does $jsonObj.objects.sbbuildid not resolve the correct value when you try it on your own machine, or...?
-5
$sb_build_id = $build_info.Substring($build_info.IndexOf("sbbuildid") + 11, 8).trim()

Put whole string in $build_info

3 Comments

That's a pretty fragile solution, it captures the space in front of the ID, and breaks once you reach a build id above 9999999 or below 1000000
I am not sure why it is capturing space. I used a trim() after getting the value.
using the built in ConvertFrom-Json is a much much better way to go. It improves readability and reduces error prone substring operations.

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.