0

I have the following array which I would like to access the key-value pairs in my Jenkinsfile.

[
    {
        "AppName": "app1",
        "version": "1.0.0.1"
    },
    {
        "AppName": "app2",
        "version": "1.0.0.2"
    },
    {
        "AppName": "app3",
        "version": "1.0.0.3"
    }
]

I would like to get the app name and version and download the applications from my repository via a curl command. I already tried this solution but doesn't work. I get the error on my jenkins console hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String . I don't know what else I am doing wrong. My plan is to download the apps with following URL for each app name and version from the array.

sh "curl https://example.com/${AppName}/${version}/${AppName}-${version}.zip -O"

Here is my jenkinsfile

node { 
    stage ('checkou') {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [[path: 'projectA/testProjectfile.json']]]], submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://myrepository.com/apps/projectA.git']]])
    }
    stage ('read file') {
        def data = readFile(file: "${WORKSPACE}/projectA/testProjectfile.json")
        echo "$data"
        def list = new JsonSlurper().parseText( data )
        list.each { println it }
        
    }
    
    stage ('download app') {
        sh "curl https://example.com/${AppName}/${version}/${AppName}-${version}.zip -O"
    
    }
}

That is my Jenkinsfile and here is the log when I run the pipeline

[Pipeline] echo
[
    {
      "AppName": "app1",
      "version": "1.0.0.1"
    },
    {
      "AppName": "app2",
      "version": "1.0.0.2"
    },
    {
      "AppName": "app3",
      "version": "1.0.0.3"
    }
]
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String
    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143)
    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)
    at net.sf.json.groovy.JsonSlurper.parseText(JsonSlurper.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
7
  • Why is your question titled like the iterating is the problem, when the problem is parsing the JSON? Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. Commented Jan 19, 2021 at 20:32
  • my quest is updated. Hope that that is understandable now Commented Jan 19, 2021 at 20:43
  • Most likely a Serialization error, due to CPS. Try using JsonSlurperClassic Commented Jan 20, 2021 at 0:46
  • Why you are not using readJson instead of readFile? Commented Jan 20, 2021 at 2:04
  • @daggett I tried using readJson as you said but I keep on getting thesame error. I do not know at the moment what causes the error Commented Jan 20, 2021 at 15:51

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.