1

I am facing this error Caused: java.io.NotSerializableException: groovy.json.JsonBuilder while building a jenkins pipeline. However the same code is working in jenkins scriptler. Should I do any changes on this code to write a Json file through jenkins pipeline

def vars = ["a": 1,"b": 2,"c": 3,"d": 4,]
def json = new groovy.json.JsonBuilder()
def final_out = json "sample": vars
json "file": final_out
def file = new File("/tmp/sample.json")                                   
file.write(groovy.json.JsonOutput.prettyPrint(json.toString()))

Sample output looks like:

{
    "file": {
        "sample": {
             "a": 1,
             "b": 2,
             "c": 3,
             "d": 4
        }
    }
}
6
  • 1
    there is a native jenkins step writejson - use it instead Commented Feb 10, 2023 at 16:21
  • 1
    stackoverflow.com/questions/44707265/… Commented Feb 10, 2023 at 16:51
  • @daggett, i tried with the native jenkins writejson method, but I got this error groovy.lang.MissingMethodException: No signature of method: Script1.writeJson() is applicable for argument types: (java.util.LinkedHashMap) Commented Feb 13, 2023 at 9:01
  • 1
    jenkins.io/doc/pipeline/steps/pipeline-utility-steps/… Commented Feb 13, 2023 at 13:39
  • it should be writeJSON Commented Feb 13, 2023 at 13:39

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.