-2

I have the following JSON object:

{
  "_class": "hudson.model.FreeStyleBuild",
  "actions": [
    {
      "_class": "hudson.model.CauseAction",
      "causes": [
        {
          "_class": "hudson.model.Cause$UserIdCause",
          "shortDescription": "Started by user SYSTEM",
          "userId": "SYSTEM",
          "userName": "SYSTEM"
        },
        {
          "_class": "hudson.model.Cause$UserIdCause",
          "shortDescription": "Started by user SYSTEM",
          "userId": "SYSTEM",
          "userName": "SYSTEM"
        },
        {
          "_class": "hudson.model.Cause$UserIdCause",
          "shortDescription": "Started by user SYSTEM",
          "userId": "SYSTEM",
          "userName": "SYSTEM"
        },
        {
          "_class": "com.sonyericsson.rebuild.RebuildCause",
          "shortDescription": "Rebuilds build #1",
          "upstreamBuild": 1,
          "upstreamProject": "gcimpoies-test5",
          "upstreamUrl": "job/gcimpoies-test5/"
        },
        {
          "_class": "com.sonyericsson.rebuild.RebuildCause",
          "shortDescription": "Rebuilds build #1",
          "upstreamBuild": 1,
          "upstreamProject": "gcimpoies-test5",
          "upstreamUrl": "job/gcimpoies-test5/"
        }
      ]
    },
    {
      "_class": "hudson.model.CauseAction",
      "causes": [
        {
          "_class": "com.sonyericsson.rebuild.RebuildCause",
          "shortDescription": "Rebuilds build #1",
          "upstreamBuild": 1,
          "upstreamProject": "gcimpoies-test5",
          "upstreamUrl": "job/gcimpoies-test5/"
        }
      ]
    },
    {

    },
    {
      "_class": "com.sonyericsson.jenkins.plugins.bfa.model.FailureCauseBuildAction"
    },
    {

    },
    {

    },
    {

    }
  ],
  "artifacts": [

  ],
  "building": false,
  "description": null,
  "displayName": "#2",
  "duration": 15,
  "estimatedDuration": 61,
  "executor": null,
  "fullDisplayName": "gcimpoies-test5 #2",
  "id": "2",
  "keepLog": false,
  "number": 2,
  "queueId": 6,
  "result": "FAILURE",
  "timestamp": 1522153325922,
  "url": "http://localhost:8080/job/gcimpoies-test5/2/",
  "builtOn": "",
  "changeSet": {
    "_class": "hudson.scm.EmptyChangeLogSet",
    "items": [

    ],
    "kind": null
  },
  "culprits": [

  ]
}

And I want to extract the username: SYSTEM

I tried using JsonSlurper but with no luck. I believe the right way to go is using JSONObject instead.

Any help is much appreciated.

Biffen
  • 6,249
  • 6
  • 28
  • 36
George Cimpoies
  • 884
  • 2
  • 14
  • 26

2 Answers2

0

You can use Google Gson for parsing json in java. Here I am pasting the code to convert json into Map. So that you can simply access json as map object.

Map map = gson.fromJson(jsonString, Map.class);
System.out.println(map);

For more info, please read this thread.

SV Madhava Reddy
  • 1,858
  • 2
  • 15
  • 33
0
Gson gson = new Gson();
    Map buildDetailsMap;
    buildDetailsMap = gson.fromJson(json, Map.class);
    if (buildDetailsMap != null) {
        List actions = (List) buildDetailsMap.get("actions");
        Map actionZero = (Map) actions.get(0);
        List causes = (List) actionZero.get("causes");
        Map causeZero = (Map) causes.get(0);
        username = (String) causeZero.get("userName");
    }

This is how I did it. Thanks SV Madhava Revy

George Cimpoies
  • 884
  • 2
  • 14
  • 26