I have a Jenkins job which runs on a slave node on another machine, both master and slave run on windows. The slave node is being run as a windows service. I am unable to read a json file from workspace in the slave node and parse it. The JSON file structure is like given below. I need to read the EmailProperties object and modify each of the properties and save the json file again. I have tried multiple suggestions on readFile() & readJSON(), but none of them is working. Any help on this would be highly appreciated.

JSON file:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
  "AllowedHosts": "*",
  "EmailProperties": {
      "MailPort": "587",
      "MailServer": "mydomainmailserver.outlook.com",
      "Password": "password123$",
      "Sender": "company@domain.com"

Properties file:

json_dev_EmailProperties.MailPort = 663
json_dev_EmailProperties.MailServer = devmailserver.outlook.com
json_dev_EmailProperties.Password = DevPassword123$
json_dev_EmailProperties.Sender = company@devmailserver.outlook.com

1 Answer 1


Following is the code that worked for me, if there is any other better way to do this, suggestions are most welcome.

Main Method:

def transformjsonfile(propertiesPath, jsonfilepath) {

  // Load Properties file 
  Properties props = new Properties()
  props.load(new StringReader(readFile(propertiesPath)))

  // Load json file
  def json = jsonParseFile(jsonfilepath)

  // Transform json
  json = transformValues(props, json)
  // Write back the tranformed config to the json file

Helper methods:

def jsonParseFile(def jsonfilepath) {
    def fileContent = readFile(file: jsonfilepath, encoding: "UTF-8")
    def jsonSlurper = new JsonSlurperClassic()
    def data = jsonSlurper.parseText(fileContent.replaceAll("\\s", "").replaceAll("\uFEFF", ""))
    return data

def formatAndWriteBackJson(json)
  def jsonOut = new JsonBuilder(json).toPrettyString()
  writeFile file: jsonfilepath, text: jsonOut.toString()

def transformValues(props, json) {
  for (String name: props.keySet()) {
    String givenValue = props.getProperty(name);
    String value = givenValue.trim();
    if (name.startsWith("json_${env.Environment}_")) {
      splitarray = name.split("json_${env.Environment}_")
      jsonvalue = splitarray[1]
      key = "${jsonvalue}"
      transformvalue = "${value}"
      def someString = "${key}"
      def someChar = '.'
      int count = 0;
      for (int i = 0; i < someString.length(); i++) {
        if (someString.charAt(i) == someChar) {
      keyvalue = "json"
      splitarray = key.split('\\.')
      lab1: for (int j = 0; j <= count; j++) {
        keyvalue = keyvalue + "." + splitarray[j]
        jsonvalue = Eval.me('json', json, "${keyvalue}")
        if (jsonvalue == null) {
          break lab1;
        if (jsonvalue != null) {
          println 'condition met'
          if (j == count) {
            Eval.me('json', json, "${keyvalue} ='$transformvalue'")
  return json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.