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

Writing variables in JSON using writeJson of Pipeline Utility

XMLWordPrintable

      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.

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

              Created:
              Updated: