Read and parse json file from workspace on slave node



  • 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:

    [transformation]
    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
    


  • 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
    formatAndWriteBackJson(json)
    }

    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) {
    count++;
    }
    }
    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
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2