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.
edited Feb 09 '14, 4:26 p.m.

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 forum
            myjson = project.getProperty("commandoutput");
            echo = project.createTask("echo");
            echo.setMessage("Javascript found this:" + myjson);

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: ReferenceError: "JSON" is not defined. (<Unknown source>#3) in <Unknown source> at line number 3
    at com.sun.script.javascript.RhinoScriptEngine.eval(

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() ?


Accepted answer

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

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

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

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.