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
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
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:
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.