1

I have a JSON file (https://github.com/conde-nast-international/cnid-tech-tests/blob/master/data/article.json) that has five array fields for an article: id, title, body, cover image, and url. But the body array has three embedded array objects (plaintext, pull_quote, and h2) that are not showing up.

I have no idea how to tackle this.

HTML:

<div id="container">
  <table id="article_table" class="table table-bordered ">
    <tr>
      <th>ID</th>
      <th>Title</th>
      <th>Body</th>
      <th>Cover Image</th>
      <th>Url</th>
    </tr>
  </table>
</div>

JavaScript:

<script>
  $(document).ready(function() {
    $.getJSON("article.json", function(data){
      console.log(data) //just to log in console as well
      var article_data = '';
      $.each(data, function(key, value){
        article_data += '<tr>';
        article_data += '<td>'+value.id+'</td>';
        article_data += '<td>'+value.title+'</td>';
        article_data += '<td>'+value.body+'</td>';
        article_data += '<td>'+value.cover+'</td>';
        article_data += '<td>'+value.url+'</td>';
        article_data += '</tr>';
      });
      $('#article_table').append(article_data);
    });
  });
</script>

Whole page view:

enter image description here

Section with embedded array objects that wont display:

enter image description here

3
  • JSON.parse your results. Commented Aug 10, 2017 at 17:28
  • @jmargolisvt do you have anymore detail to that answer? I would really appreciate a code example Commented Aug 10, 2017 at 17:30
  • It looks parsed already, its just that body is an array of objects and not a string. Commented Aug 10, 2017 at 17:33

1 Answer 1

2

Since value.body is an array of objects, you will have to iterate through it and display some properties of each object.

  $.each(parsedData, function(key, value){
    article_data += '<tr>';
    article_data += '<td>'+value.id+'</td>';
    article_data += '<td>'+value.title+'</td>';
    article_data += '<td>';
    $.each(value.body, function (index, el) {
      if (el.type == 'plaintext') {
        // do something special if it's plaintext (?)
      } else if (el.type == 'h2') {
        // put the content in an h2 (?)
      }
      // just display the content
      article_data += el.body;
    });
    article_data += '</td>';
    article_data += '<td>'+value.cover+'</td>';
    article_data += '<td>'+value.url+'</td>';
    article_data += '</tr>';
  });
Sign up to request clarification or add additional context in comments.

10 Comments

This is amazing! Thank you! I'm taking your suggestion to add styles according to the if else statement and I'm using the following: plaintext.style.color = red; but it doesn't seem to be working. Do you know what's wrong here?
plaintext isn't a dom element so it doesn't have a style property. You're concatenating the content into a string, so I would say add something like article_data += "<span style='color:red'>" + el.body + "</span>";
I tried that but that didn't work. I currently have if (el.type == 'plaintext') { article_data += "<span style='color:red; font-weight:bold; display:block;'></span>"; } but the only thing taking effect is the display:block;
You have to put the content inside the span
if (el.type == 'plaintext') { article_data += "<span 'plaintext' style='color:red; display:block;'></span>"; } doesn't seem to be working either unfortunately
|

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.