Search code examples
javascripthtmljsontcljson2html

JSON2HTML returning error when file is passed from Tcl


I'm using a custom Tcl script that reads JSON from a file and returns it inside a JavaScript script. I've written a simple transform for this JSON, but when I execute it, I get an error. The funny thing is, when I paste the very same JSON inline, it works. I know that there must be an issue with the Tcl script, but I can't seem to figure out what it is. Any suggestions?

var datJSON = Tcl_Script_to_get_JSON;

//var datJSON = some_inline_JSON       THIS WORKS!!
 var trans = [
      { "tag" : "div", "id" : "reportTitle", "html" : "Report: ${Name}" }, //report title

      { //general Details, date, time, revision number
            "tag" : "ul", "children" : [
                                    {"tag" : "li", "class" : "generalDetails", "html" : "Created on : ${Date}"},
                                    {"tag" : "li", "class" : "generalDetails", "html" : " at : ${Time}"},
                                    {"tag" : "li", "class" : "generalDetails", "html" : "Revised for : ${RevisionNumber}"}
                                ]
        }

      ];


  //run the JSON through the json2html transform
  var output = json2html.transform(datJSON, trans);
  $("#main").append(output);

Solution

  • The issue is that JSON2HTML is being used outside of a browser context.

    JSON2HTML uses JSON.parse() in order to parse the JSON, which is a function native to browsers. Outside of that, we'll have to use jQuery or whatever library helps, like $.parseJSON().

    That does the trick. I should have checked the source thoroughly.