Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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

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

0 votes


Accepted answer

Permanent link
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

0 votes

Comments

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

I gotta remember this. neat 

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,943
× 562
× 152
× 25

Question asked: Feb 09 '14, 12:05 p.m.

Question was seen: 9,031 times

Last updated: Feb 10 '14, 5:43 p.m.

Confirmation Cancel Confirm