简体   繁体   中英

How to modify JSON property on jenkins workspace using groovy?

Problem: I'm reading the property called 'version' in a JSON file and printing it successfully. Now I am trying to modify it, and modify the file, ie increment the version number using groovy.

My file is pretty simple:

{
    "version":"1.0.0"
}

So far I tried using the writeJSON function to manipulate the version number.

def pack = scope.readJSON file: "path/to/json/file.json"
String currVersion = "${pack.version}"
// The above code works...

// The code below does not
pack['version'] = "1.2.0"
scope.writeJSON file: "path/to/json/file.json", json: pack

Expected jenkins build to pass and the json file to get modified but I get the following error message:

groovy.lang.MissingPropertyException: No such property: version for class

All you need to do is to set a parameter returnPojo: true. Which will transforms the output into a POJO type (LinkedHashMap or ArrayList) before returning it.

By default, the parameter is set to false and a JSON object (JSONObject or JSONArray from json-lib) is returned.

def pack = scope.readJSON file: "path/to/json/file.json", returnPojo: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM