Robert Thompson
Robert Thompson

Reputation: 43

Parsing Json in Json

I'm working in a Jenkinsfile and trying to parse the following return output. I can get the uuid but I can't get name. Looking for some guidance.

Jenkinsfile

    node('ansible'){
      stage('Get VM List'){
        def content = sh (returnStdout: true, script: "curl -X GET --header 'Content-Type: application/json' --header 'Accept: application/octet-stream' 'http://someurlapi'").trim()
        def vmList = readJSON text: content;

        //Works
        echo vmList[0].uuid
}}

Return Output

[
  {
    "num": XX,
    "ip": "XX.XX.XX.XX",
    "type": "KVM",
    "name": "machinename",
    "state": "Running",
    "ram": 4096,
    "ram-display": "4 GiB",
    "zpool": {
      "name": "zpool",
      "compression": "lz4",
      "mountpoint": "\/mnt",
      "mounted": true
    },
    "uuid": "d7622bd3-ed3d-5000-ae01-89ab294933r1",
    "autostart": false,
    "cpu": 2
  }]

Upvotes: 0

Views: 731

Answers (1)

Robert Thompson
Robert Thompson

Reputation: 43

I figured it out I changed it to

echo vmList[0]["name"]

Upvotes: 2

Related Questions