0

Currently I am calling 2 json files, I'd like instead of calling 2 json file to call only one and output that in html...

One (carousel data) is like that:

[
{
    "carouseltitle": "65",
    "carouseldescription": "genegregerggrators connected<br/> to the grid",
    "carouselimg": "assets/img/stories.jpg",
    "carousellink": "erg"
},
{
    "carouseltitle": "634345",
    "carouseldescription": "3545453 connected<br/> to the grid",
    "carouselimg": "assets/img/stories.jpg",
    "carousellink": "erg"
},
{
    "carouseltitle": "HELLO",
    "carouseldescription": "HELLO connected<br/> to the grid",
    "carouselimg": "assets/img/stories.jpg",
    "carousellink": "erg"
}
]

the other one (number data) like this:

[
{
  "number": "65",
  "description": "generators connected<br/> to the grid",
  "imageUrl": "assets/img/Plug.svg"
},
{
  "number": "314MW",
  "description": "of energy controlled<br/> in the UK",
  "imageUrl": "assets/img/Lightning_Bolt.svg"
},
{
"number": "£100m",
"description": "CAPEX avoided",
"imageUrl": "assets/img/Money.svg"
},
{
  "number": "6mths",
  "description": "average time to connection",
  "imageUrl": "assets/img/Calendar.svg"
},
{
  "number": "177kt",
  "description": "CO2 avoided",
  "imageUrl": "assets/img/Cloud.svg"
}  
]

To output the data, I'm calling the file apart so via:

$.getJSON('js/carousel.json', function(data) {
    var htmlText = '';
    for ( var key in data ) {
        htmlText += '<article>';                               
        htmlText += '<div class="article-wrapper">';                  
        htmlText += ' <img src="' + data[key].carouselimg + '">';                  
        htmlText += '<div class="title-overlay">';
        htmlText += '<h2>' + data[key].carouseltitle + '</h2>';

        htmlText += '</div>';
        htmlText += '<div class="content">';
        htmlText += '<p>' + data[key].carouseldescription + '</p>';                    
        htmlText += '</div>';
        htmlText += '</div>';
        htmlText += '</article>';
    }
    $('.article-stories').append(htmlText);    
 });

 $.getJSON('js/data.json', function(data) {
     var htmlText = '';
     for ( var key in data ) {
         htmlText += '<div class="row numbers">';                               
         htmlText += '<div class="number-row">';                  
         htmlText += '<div class="col-one empty"></div>';
         htmlText += '<div class="col-one"> <img src="' + data[key].imageUrl + '"></div>';
         htmlText += '<div class="col-nine ">';
         htmlText += '<div class="number">' + data[key].number + '</div>';                    
         htmlText += '<div class="description"><h3>' + data[key].description + '</h3></div>';                    
         htmlText += '</div>';
         htmlText += '<div class="col-one empty"></div>';
         htmlText += '</div>';
         htmlText += '<div class="horizontal-center">';                  
         htmlText += '<span class="divider"></span>';                  
         htmlText += '</div>';
         htmlText += '</div>';
     }
     $('.number-wrapper').append(htmlText);
 });

How can I instead have all the data in a single json data file, and call them ?

Id like to make this much cleaner.

Thank a lot !!

4 Answers 4

1
{
 "carouselData": [
    {
        "carouseltitle": "65",
        "carouseldescription": "genegregerggrators connected<br/> to the grid",
        "carouselimg": "assets/img/stories.jpg",
        "carousellink": "erg"
    },
    {
        "carouseltitle": "634345",
        "carouseldescription": "3545453 connected<br/> to the grid",
        "carouselimg": "assets/img/stories.jpg",
        "carousellink": "erg"
    },
    {
        "carouseltitle": "HELLO",
        "carouseldescription": "HELLO connected<br/> to the grid",
        "carouselimg": "assets/img/stories.jpg",
        "carousellink": "erg"
    }
 ],
 "numberData": [
    {
    "number": "65",
    "description": "generators connected<br/> to the grid",
    "imageUrl": "assets/img/Plug.svg"
    },
    {
    "number": "314MW",
    "description": "of energy controlled<br/> in the UK",
    "imageUrl": "assets/img/Lightning_Bolt.svg"
    },
    {
    "number": "£100m",
    "description": "CAPEX avoided",
    "imageUrl": "assets/img/Money.svg"
    },
    {
    "number": "6mths",
    "description": "average time to connection",
    "imageUrl": "assets/img/Calendar.svg"
    },
    {
    "number": "177kt",
    "description": "CO2 avoided",
    "imageUrl": "assets/img/Cloud.svg"
    }  
]
}

$.getJSON('js/carousel.json', function(data) {
  var htmlText = '';
  for ( var key in data.carouselData ) {
    htmlText += '<article>';                               
    htmlText += '<div class="article-wrapper">';                  
    htmlText += ' <img src="' + data.carouselData[key].carouselimg + '">';                  
    htmlText += '<div class="title-overlay">';
    htmlText += '<h2>' + data.carouselData[key].carouseltitle + '</h2>';

    htmlText += '</div>';
    htmlText += '<div class="content">';
    htmlText += '<p>' + data.carouselData[key].carouseldescription + '</p>';                    
    htmlText += '</div>';
    htmlText += '</div>';
    htmlText += '</article>';
}
$('.article-stories').append(htmlText);  
 htmlText = '';
 for ( var key in data.numberData ) {
     htmlText += '<div class="row numbers">';                               
     htmlText += '<div class="number-row">';                  
     htmlText += '<div class="col-one empty"></div>';
     htmlText += '<div class="col-one"> <img src="' + data.numberData[key].imageUrl + '"></div>';
     htmlText += '<div class="col-nine ">';
     htmlText += '<div class="number">' + data.numberData[key].number + '</div>';                    
     htmlText += '<div class="description"><h3>' + data.numberData[key].description + '</h3></div>';                    
     htmlText += '</div>';
     htmlText += '<div class="col-one empty"></div>';
     htmlText += '</div>';
     htmlText += '<div class="horizontal-center">';                  
     htmlText += '<span class="divider"></span>';                  
     htmlText += '</div>';
     htmlText += '</div>';
 }
});

Description: Create json object with 2 key(carouselData, numberData).both contain array. We can loop these array separatly

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

Comments

0
         var a =   [
            {
                "carouseltitle": "65",
                "carouseldescription": "genegregerggrators connected<br/> to the grid",
                "carouselimg": "assets/img/stories.jpg",
                "carousellink": "erg"
              },
              {
                "carouseltitle": "634345",
                "carouseldescription": "3545453 connected<br/> to the grid",
                "carouselimg": "assets/img/stories.jpg",
                "carousellink": "erg"
              },
              {
                "carouseltitle": "HELLO",
                "carouseldescription": "HELLO connected<br/> to the grid",
                "carouselimg": "assets/img/stories.jpg",
                "carousellink": "erg"
              }
            ]
   var b= [
    {
      "number": "65",
      "description": "generators connected<br/> to the grid",
      "imageUrl": "assets/img/Plug.svg"
    },
    {
      "number": "314MW",
      "description": "of energy controlled<br/> in the UK",
      "imageUrl": "assets/img/Lightning_Bolt.svg"
    },
    {
    "number": "£100m",
    "description": "CAPEX avoided",
    "imageUrl": "assets/img/Money.svg"
    },
    {
      "number": "6mths",
      "description": "average time to connection",
      "imageUrl": "assets/img/Calendar.svg"
    },
    {
      "number": "177kt",
      "description": "CO2 avoided",
      "imageUrl": "assets/img/Cloud.svg"
      }
    ]

in that file write

module.exports = {
a :a,
b:b
}

save this in one file and import where ever needed.Require this file like

var obj = require('/filename')
var firstJson = obj.a;
var secondJson = obj.b

4 Comments

if any doubt ask ping me
Although that might work in a node.js or ES6 compliant environment it won't work for example on IE11, unless using Babel or such.
how can i ping you?
is that fixed ?
0

I recommend you to do separate requests as you can render each block when json is downloaded and handle errors to each json file.

Possible duplicate of this post

Comments

0

You could just make a associative array json made of regular arrays , such as:

{
   carousel: [{
    "title": "65",
    "description": "genegregerggrators connected<br/> to the grid",
    "img": "assets/img/stories.jpg",
    "link": "erg"
  }],
  numbers: [{
    "number": "65",
    "description": "generators connected<br/> to the grid",
    "imageUrl": "assets/img/Plug.svg"
  }]
}

And then load it as:

  $.getJSON('js/data.json', function(data) {
    var htmlText = '';

       for ( var key in data.carousel ) {
         htmlText +=
            '<article>'                               
          +=   '<div class="article-wrapper">'                  
          +=     '<img src="' + data.carousel[key].img + '">'                  
          +=     '<div class="title-overlay">'
          +=       '<h2>' + data.carousel[key].title + '</h2>'
          +=     '</div>'
          +=     '<div class="content">'
          +=       '<p>' + data.carousel[key].description + '</p>'                  
          +=     '</div>'
          +=   '</div>'
          += '</article>';
       }

      $('.article-stories').append(htmlText);

      htmlText = '';
      for ( var key in data.numbers ) {
          htmlText +=
            '<div class="row numbers">'                      
          +   '<div class="number-row">'                 
          +     '<div class="col-one empty"></div>'
          +     '<div class="col-one">'
          +       '<img src="' + data.numbers[key].imageUrl + '">'
          +     '</div>'
          +     '<div class="col-nine ">'
          +       '<div class="number">' + data.numbers[key].number + '</div>'                    
          +     '<div class="description"><h3>' + data.numbers[key].description + '</h3></div>'                    
          +   '</div>'
          + '<div class="col-one empty"></div>'
          + '</div>'
          + '<div class="horizontal-center">'                  
          +   '<span class="divider"></span>'                  
          + '</div>'
          + '</div>';
     }
     $('.number-wrapper').append(htmlText);
});

Edit: Modified JSON in order to avoid misunderstandings.
Edit 2: Fixed some remaining semicolons.

1 Comment

Hi alvaro, Thansk a lot. seems there is a pb with the json Is the structure correct from your code ?

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.