Search code examples
groovyjenkins-groovygroovyshellgroovy-console

Fetch value from json array key starts with @


I am trying to access the value of

@angular/core

from json file looks like bellow

"dependencies": {
   "@angular/animations": "^6.1.0",
   "@angular/common": "^6.1.0",
   "@angular/compiler": "^6.1.0",
   "@angular/core": "^6.1.0",
   "@angular/forms": "^6.1.0",
   "@angular/http": "^6.1.0",
   "@angular/platform-browser": "^6.1.0",
   "@angular/platform-browser-dynamic": "^6.1.0",
   "@angular/router": "^6.1.0",
   "core-js": "^2.5.4",
   "rxjs": "~6.2.0",
   "zone.js": "~0.8.26"
},

the code which i have tried is

    def packageString = readFile("app/package.json")  // read the json file
    def parsedPackageString =  new groovy.json.JsonSlurperClassic().parseText(packageString)

    echo "My Angular Project version $parsedPackageString.dependencies.@angular/core"

but it is printing all the values of dependencies

My expected result is

"^6.1.0",


Solution

  • You need only to specify property for parsedPackageString:

    def parsedPackageString =  new groovy.json.JsonSlurperClassic().parseText(packageString).dependencies.'@angular/core'