Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Last active June 3, 2018 16:37
Show Gist options
  • Save ryanguill/27f8212d9c6c1e84383b8948388b972b to your computer and use it in GitHub Desktop.
Save ryanguill/27f8212d9c6c1e84383b8948388b972b to your computer and use it in GitHub Desktop.
<cfscript>
function prettyPrintJSON (inputJSON) {
var engine = createObject("java","javax.script.ScriptEngineManager").init().getEngineByName("nashorn");
engine.eval("
function prettyPrintJSON (data) {
return JSON.stringify(JSON.parse(data), null, '\t');
}
");
return engine.invokeFunction("prettyPrintJSON", [inputJSON]);
}
</cfscript>
<cfsavecontent variable="myJSON">
{"a":1,"b":"foo","c":[true,false,null,"null",{"d":{"e":130000,"f":1.3e5}}]}
</cfsavecontent>
<cfoutput>
<textarea style="width:100%; height: 100%">#prettyPrintJSON(myJSON)#</textarea>
</cfoutput>
@ryanguill
Copy link
Author

verified working on lucee 4.5

@ryanguill
Copy link
Author

verified now on ACF 11 and 2016

@agentfitz
Copy link

agentfitz commented Sep 30, 2016

Variable ENGINE is undefined on ColdFusion 10.0.20

image

image

@ryanguill
Copy link
Author

10 probably doesnt work because of an older JVM, but thats just a guess.

@ryanguill
Copy link
Author

ryanguill commented Apr 28, 2017

Good introduction to how you can work back and forth between java and javascript here: http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/

and http://www.n-k.de/riding-the-nashorn/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment