Reputation: 211
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, i.e 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
Upvotes: 2
Views: 3295
Reputation: 291
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
Upvotes: 2