Read json from jenkins

Rene Sørensen picture Rene Sørensen · Mar 6, 2018 · Viewed 25.8k times · Source

Im trying to read a json file from within a jenkinsfile with grovvy script. Im using the pipeline-utility-steps-plugin, which allows to read the json file as string with the following.

def projects = readJSON file: "${env.WORKSPACE}\\Projects.json"

after reading the doc, i was thinking i could get out with something like this, but i surely do something wrong because result is null?

projects.project[1].name

Now my problem is i cant seem to figure out how i get the name of number 2 out? Please help me out

Content of the Projects.json

    {
  "projects": {
    "project": [
      {
        "name": "PackingStation",
        "solution": "PackingStation\\BLogic.Applications.PackingStation.sln",
        "analysisFiles": "BLogic.Applications.PackingStation.exe"
      },
      {
        "name": "MasterData",
        "solution": "MasterData\\BLogic.Applications.MasterData.sln",
        "analysisFiles": "BLogic.Applications.MasterData.exe"
      },
      {
        "name": "OrderManager",
        "solution": "OrderManager\\BLogic.Applications.OrderManager.sln",
        "analysisFiles": "BLogic.Applications.OrderManager.exe"
      }
    ]
  }
}

Answer

SV Madhava Reddy picture SV Madhava Reddy · Mar 6, 2018

You are accessing it wrong. projects in projects.project[1].name refers to the variable defined here def projects = readJSON file: "${env.WORKSPACE}\\Projects.json".

You have again inner json key as projects. So please use projects.projects.project[1].name to access the value. Hope this helps.