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

18
  • stackoverflow.com/questions/5180382/… Commented Nov 16, 2017 at 20:38
  • Check out this example jsfiddle.net/7MRx6/338 Commented Nov 16, 2017 at 20:39
  • @nikunjMnage I tried the example on stackoverflow.com/questions/17066636/…, but it gives [object, Object] as display. Inside the function(data), the 'tr' variable was created Commented Nov 16, 2017 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); Commented Nov 16, 2017 at 20:48
  • @Piyin I added the 'plugin json2html', with transform, now i get output like <li>a1 (0.2)</li><li> a2 (0.3)</li> used $('#d2').html(json2html.transform(data, transform)); where var transform = {'<>':'li','html':'${tag} (${relevance})'}; Is it possible to remove those tags from the output Commented Nov 16, 2017 at 21:03

1 Answer 1

1

You should traverse the data and print it in table format. First, make sure the element with id dJson is a <table> element. Then, you can change your success callback to something like:

function(data) {
  console.log(Object.keys(data).length);
  var html = '<tr><th>Tag</th><th>Relevance</th></tr>';
  for(var i=0; i<data.length; i++){
    html +=
      '<tr>' +
        '<td>' +
          data[i].tag +
        '</td>' +
        '<td>' +
          data[i].relevance +
        '</td>' +
      '</tr>'
    ;
  }
  d2.innerHTML = html;
}

Here you have a working example (without the $.post call): https://jsfiddle.net/cLbqmwa4/

Sign up to request clarification or add additional context in comments.

2 Comments

The <table id="dJson"></table> was not working. So, i changed it to div <div id="dJson"></div> and also $('#dJson').html(html); Now, it works, thanks
It looks like you have CSS problems in the code, the <table> should work without problems. If you check the JSFiddle link you see how it works with <table> but not with <div>. Anyway, glad it worked for you and you're welcome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.