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)
Leroi
  • 349
  • 4
  • 21
  • 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. – cfrick Jan 19 '21 at 20:32
  • my quest is updated. Hope that that is understandable now – Leroi Jan 19 '21 at 20:43
  • Most likely a Serialization error, due to CPS. Try using `JsonSlurperClassic` – Patrice M. Jan 20 '21 at 00:46
  • Why you are not using readJson instead of readFile? – daggett Jan 20 '21 at 02: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 – Leroi Jan 20 '21 at 15:51
  • you can't have the same error unless you still using jsonslurper. check this: https://stackoverflow.com/questions/44707265/create-json-strings-from-groovy-variables-in-jenkins-pipeline/44718808#44718808 – daggett Jan 20 '21 at 17:49
  • it now works with readJson. this is kind of crazy from my side but I don't know what happened with the original json file. I deleted the old one created a new one and everything now works :). thanks for your help – Leroi Jan 21 '21 at 07:55

0 Answers0