Vam Vam - 19 days ago 7
JSON Question

json file parsing in groovy

the below code is working fine in windows (groovy script shell of jenkins) and when i execute it on groovy script shell of jenkins in linux its throwing the error

groovy.lang.MissingMethodException: No signature of method: sun.nio.fs.UnixPath.withReader() is applicable for argument types: (Script1$_run_closure1) values: [Script1$_run_closure1@71c12adb]
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script1.run(Script1.groovy:8)
at org.jenkinsci.plugins.scriptler.util.GroovyScript.call(GroovyScript.java:122)
at hudson.remoting.LocalChannel.call(LocalChannel.java:45)
at org.jenkinsci.plugins.scriptler.util.ScriptHelper.runScript(ScriptHelper.java:124)
at org.jenkinsci.plugins.scriptler.util.ScriptHelper.runScript(ScriptHelper.java:89)
at org.jenkinsci.plugins.scriptler.ScriptlerManagement.doTriggerScript(ScriptlerManagement.java:470)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)

CODE:
i pass the value for Select_Folders

import groovy.json.JsonSlurper
import java.nio.file.Paths

JsonSlurper slurper = new JsonSlurper()
def folder_struct

Paths.get('C:\\Json_Results\\results.json').withReader { reader ->
folder_struct = slurper.parse(reader)
}


return folder_struct [Select_Folders]

Could any one please help me where the problem is? ofcorse i have changed the path 'C:\Json_Results\results.json' when running on linux to '/home/project/results.json'

Answer

So Path extensions were only added to Groovy in v2.3.0

I suspect you have an old version of Groovy on your Jenkins nodes.

You should just be able to use a File instead:

def folder_struct = new File('C:\\Json_Results\\results.json').withReader { reader ->
    slurper.parse(reader)
}