6

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"
      }
    ]
  }
}
Rene Sørensen
  • 99
  • 1
  • 1
  • 8

1 Answers1

13

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.

SV Madhava Reddy
  • 1,858
  • 2
  • 15
  • 33
  • Could you point me to something that help me if i wanna iterate through the projects.projects.project collection? – Rene Sørensen Mar 06 '18 at 13:03
  • [https://stackoverflow.com/questions/3855232/parsing-array-of-json-arrays-in-groovy](https://stackoverflow.com/questions/3855232/parsing-array-of-json-arrays-in-groovy) – SV Madhava Reddy Mar 06 '18 at 13:48
  • indeed an unfortunate naming "projects", there is one too many objects named "projects" – lowtech Feb 11 '21 at 15:33