0

I get the JSON output on the UI using the below function

$("#idvar").click(function(){
  var d1 = document.getElementById("dText");
  var d2 = document.getElementById("dJson");
  var mytext = d1.textContent;
  alert(mytext);
  $.post(
    url,
    {doc: mytext},
    function(data) {
      console.log(Object.keys(data).length);
      d2.textContent = data;
    }
  );
});

where d1 is the displaying the uploaded input document, d2 for the output generated from processing the document on a model. Currently, I am getting json format. I tried to implement the codes in these posts (Parsing JSON objects for HTML table, Convert json data to a html table), but I get [object, Object] in the text area. Is there a way to convert the json to table.

In the URL text area i get

 [{"tag":"a1","relevance":0.5},
  {"tag":"a2","relevance":0.3},
  {"tag":"c3","relevance":0.2},{"tag":"d3,
   ...

Instead, I was hoping to get

enter image description here

davidism
  • 121,510
  • 29
  • 395
  • 339
  • https://stackoverflow.com/questions/5180382/convert-json-data-to-a-html-table?noredirect=1&lq=1 – nikunjM Nov 16 '17 at 20:38
  • Check out this example http://jsfiddle.net/7MRx6/338/ – nikunjM Nov 16 '17 at 20:39
  • @nikunjMnage I tried the example on https://stackoverflow.com/questions/17066636/parsing-json-objects-for-html-table, but it gives [object, Object] as display. Inside the function(data), the 'tr' variable was created – user8954059 Nov 16 '17 at 20:42
  • Share with us what the returned JSON looks like. You can see it by changing `d2.textContent = data;` for `d2.textContent = JSON.stringify(data);` – Piyin Nov 16 '17 at 20:48
  • @Piyin I added the 'plugin json2html', with transform, now i get output like `
  • a1 (0.2)
  • a2 (0.3)
  • ` used `$('#d2').html(json2html.transform(data, transform));` where `var transform = {'<>':'li','html':'${tag} (${relevance})'};` Is it possible to remove those tags from the output – user8954059 Nov 16 '17 at 21:03
  • Which tags do you want to remove? – Piyin Nov 16 '17 at 21:07
  • @Piyin the 'd2' is the id of the textarea where I am displaying. I thought that json2html gives a nice table on the ui. But, instead it gives with tags i.e. `
  • ` So, I removed it in `var transform = {'<>': '','html':'${tag} (${relevance})'};` Now it gives `<>a1 (0.2)>` Sorry, I am new to javascript – user8954059 Nov 16 '17 at 21:18
  • @Piyin I have been trying for almost 3 hours. Didn't know that this is too difficult – user8954059 Nov 16 '17 at 21:34
  • Oh, well, I've never used that plugin, but perhaps you're able to configure it with: `var transform = {'<>': 'tr','html':'${tag}${relevance}'};` and make sure `d2` references a `` element. Also, it's not that difficult to do by hand (without the plugin) it's just important to know the structure
    – Piyin Nov 16 '17 at 21:39
  • @Piyin Thanks, let me try that one. If it works, you could post as an answer and will accept that – user8954059 Nov 16 '17 at 21:45
  • @Piyin The 'd2' is referencing to `textarea` i.e. `` – user8954059 Nov 16 '17 at 21:46
  • Change the `id` attribute of the `textarea` (to something different to `d2`) and put this below that line: `
    `
    – Piyin Nov 16 '17 at 21:50
  • @Piyin Now, I get that `
    ` in the display output. Sorry, I am lost
    – user8954059 Nov 16 '17 at 21:56
  • @Piyin may be tables are not meant to be inside textarea – user8954059 Nov 16 '17 at 21:59
  • No, wait, I didn't say you should but the `table` inside the `textarea`. You should remove the `textarea` completely. And only have a line like `
    `. Then all the code should work (Hi, lost, I'm dad)
    – Piyin Nov 16 '17 at 22:00
  • @Piyin Thanks, tried that, but there is now no output displayed. – user8954059 Nov 16 '17 at 22:05
  • Oh, shit, I messed it up. The `table` should look like `
    `
    – Piyin Nov 16 '17 at 22:11
  • Oh, I didn't know you updated your answer. I'm going to give you an answer – Piyin Nov 16 '17 at 22:16