1

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);
  • You are going to post the (relevant parts of) the Tcl script, right? It's hard to give suggestions on it otherwise. Also, since you are confident that the issue is not with the JS code, you probably don't need to list that. – Peter Lewerin Jul 22 '15 at 07:14
  • I really wish I could. But it has nothing to do with that, I see now. Since I was using json2html outside of a browser context, I had to change JSON.parse() to $.parseJSON –  Jul 22 '15 at 07:45

1 Answers1

1

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.