It's all about the answers!

Ask a question

How can I parse JSON output in context of Jazz ANT build?


Arne Bister (2.6k12632) | asked Feb 09 '14, 12:05 p.m.
JAZZ DEVELOPER
edited Feb 09 '14, 4:26 p.m.
Jazzers,

on RTC 4.0.3 I was currently trying to do something to the JSON output of a cli command in my ANT build.
I successfully ran the cli command in the context of an <exec> task, storing it in outputproperty="commandoutput"

In a subsequent <script language="javascript"> task I can successfully do:
<script language="javascript">
< ! [CDATA[   // writing this line with extra spaces to make it appear in jazz.net forum
            myjson = project.getProperty("commandoutput");
            echo = project.createTask("echo");
            echo.setMessage("Javascript found this:" + myjson);
            echo.perform();
        ]]>   
</script>

and the correct string output will appear in my build logs.
But as soon as I added this line:
var data = JSON.parse(myjson);

the script task balked and threw this error in the build log:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "JSON" is not defined. (<Unknown source>#3) in <Unknown source> at line number 3
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:156)
[...]

How can I parse JSON data in my ANT builds using the < script > task? What would I need to include for the javascript part to recognize JSON.parse() ?

Thanks,
Arne

Accepted answer


permanent link
Arne Bister (2.6k12632) | answered Feb 09 '14, 4:29 p.m.
JAZZ DEVELOPER
Jazzers,

this is what worked for me: instead of using the JSON.parse() construct in the script task of ANT with javascript, I used this code snippet, with jsonOutput having been generated by an <exec> task before:
json = project.getProperty("jsonOutput");
var struct = eval("(" + json + ")");
// now accessing the data in the object
componentUUID = struct.workspaces[0].components[0].uuid;

It gets the job done.
Ralph Schoon selected this answer as the correct answer

Comments
Nick Edgar commented Feb 10 '14, 1:03 p.m.
JAZZ DEVELOPER

Glad you were able to get it going. Using eval is what I was going to suggest. 


sam detweiler commented Feb 10 '14, 5:43 p.m.

I gotta remember this. neat 

Your answer


Register or to post your answer.