Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-46167

Writing variables in JSON using writeJson of Pipeline Utility

      While trying to assign variables into some JSON file readed in a Pipeline Project.

       Using this JSON file file.json:

      [  
         {  
            "id":"1",
            "buildNumber":"",
            "lock":"false",
            "tag":"dbaix",
            "configurationFile":"path/someFile.properties"
         },
         {  
            "id":"2",
            "buildNumber":"",
            "lock":"true",
            "tag":"dbaix",
            "configurationFile":"path/someFile.properties"
         },
         {  
            "id":"3",
            "buildNumber":"2222222",
            "lock":"true",
            "tag":"dbaix",
            "configurationFile":"path/someFile.properties"
         }
      ]
      

      You do:

      jsonDictionary = readJSON file: "file.json"
      jsonEnvDefinition = jsonDictionary.get(0);
      buildNumberVar = "xx"
      jsonEnvDefinition['buildNumber'] = "${buildNumberVar}"
      echo "${jsonDictionary }"
      

      I am getting in the output:

      [[id:1, buildNumber:[bytes:[120, 120], strings:[, ], valueCount:1, values:[xx]], lock:true, tag:dbaix, configurationFile:path/someFile.properties], [id:2, buildNumber:, lock:true, tag:dbaix, configurationFile:path/someFile.properties], [id:3, buildNumber:2222222, lock:true, tag:dbaix, configurationFile:path/someFile.properties]]
      

      Which doesn't make sense. Instead if I hard coded the value

      jsonDictionary = readJSON file: "file.json"
      jsonEnvDefinition = jsonDictionary.get(0);
      jsonEnvDefinition['buildNumber'] = "xx"
      echo "${jsonDictionary }"
      

      I will get in the output:

      [[id:1, buildNumber:xx, lock:true, tag:dbaix, configurationFile:path/someFile.properties], [id:2, buildNumber:, lock:true, tag:dbaix, configurationFile:path/someFile.properties], [id:3, buildNumber:2222222, lock:true, tag:dbaix, configurationFile:path/someFile.properties]]

      Which make sense. I am expecting "xx" in buildNumber, but when using variables it is getting written some more complex string which I don't know how that value it is been created.

          [JENKINS-46167] Writing variables in JSON using writeJson of Pipeline Utility

          I discovered an awkward workaround, while doing this now it worked for me:

          String buildNumberVar = "xx"
          echo "buildNumberVar = ${buildNumberVar}"
          jsonEnvDefinition['buildNumber'] = " " + buildNumberVar

          Concatenating a empty String with t he buildNumberVar it worked.

          Daniel Alejandro Hernández added a comment - I discovered an awkward workaround, while doing this now it worked for me: String buildNumberVar = "xx" echo "buildNumberVar = ${buildNumberVar}" jsonEnvDefinition ['buildNumber'] = " " + buildNumberVar Concatenating a empty String with t he buildNumberVar it worked.

          jay hendren added a comment -

          You can use the inspect() method instead. This will force serialization of the string, turning the interpolated string object into a regular string object.

          For instance:

          jsonEnvDefinition['buildNumber'] = "${buildNumberVar}".inspect()
          

          Note that you will need to disable the sandbox or approve the script from the administrative console in order to use inspect().

          jay hendren added a comment - You can use the inspect() method instead. This will force serialization of the string, turning the interpolated string object into a regular string object. For instance: jsonEnvDefinition['buildNumber'] = "${buildNumberVar}".inspect() Note that you will need to disable the sandbox or approve the script from the administrative console in order to use inspect() .

          Thanks jay_hendren I found I workaround using it in this way:

          jsonEnvDefinition.put("lock", "false")

          Regardless that it is not straight forward to identify that since I had to check at the code to know what kind of objects they were using.

          Daniel Alejandro Hernández added a comment - Thanks jay_hendren I found I workaround using it in this way: jsonEnvDefinition.put("lock", "false") Regardless that it is not straight forward to identify that since I had to check at the code to know what kind of objects they were using.

          Andy Scollard added a comment -

          If you don't want to get approval to use "inspect()" as Jay suggested, "toString()" worked for me. 

          Andy Scollard added a comment - If you don't want to get approval to use "inspect()" as Jay suggested, "toString()" worked for me. 

            rsandell rsandell
            danielahcardona Daniel Alejandro Hernández
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: