Insert JSON file into Jsfiddle

Asked

Viewed 76 times

3

I am trying to solve a question with a plugin that uses jQuery but do not know how to insert a JSON file in Jsfiddle.

This JSON is a string and comes from a file .json.

1 answer

3


Depends a little on where you get JSON.

If it is a string you can paste directly into the Javascript space:

var str = '{"objeto": {"foo":"bar"}}';
var json = JSON.parse(str);

if it’s a larger file I usually put it in an ajax script:

function buscaJSON(url, cb) {
  var request = new XMLHttpRequest();
  request.open('GET', url, true);
  request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
      var data = JSON.parse(request.responseText);
      cb(null, data);
    } else {
      cb('Error: ' + request.responseText);
    }
  };
  request.onerror = function() {
    cb('Unknown error');
  };

  request.send();
}

buscaJSON('https://rawgit.com/SergioCrisostomo/version-files/master/json_example.json', function(err, json) {
  alert(JSON.stringify(json, null, 4));
});

http://jsfiddle.net/Sergio_fiddle/35hg40bz/

  • 1

    I did it! I created it on Github and then generated a Raw, just like in your example.

  • 1

    @Eduardoalmeida good! is very useful, I was glad when I discovered this possibility too.

  • Well, now I can prepare my real problem! It seems that moderators don’t like it when we say "thank you," so, Danke Schön

Browser other questions tagged

You are not signed in. Login or sign up in order to post.