-2

I have a document in JSON, with information that I intend for my addon, I found a code in this forum and tried to modify without success. What I intend is that through the function that I will leave, call this link (https://tugarepo.000webhostapp.com/lib/lib.json) so that I can see the content.

CODE:

return json.loads(openfile('lib.json',path.join('https://tugarepo.000webhostapp.com/lib/lib.json')))
Acruz
  • 1
  • 1
  • 5
  • 1
    Is that NodeJS or browser code? – PeterMader Jul 30 '17 at 14:33
  • @PeterMader This code I found here in the forum, comes within a page p and it is similar to mine and I tried to adapt to my needs. https://stackoverflow.com/questions/45269632/open-json-file-link-through-a-code – Acruz Jul 30 '17 at 14:45
  • @Acruz, your latest link features Python code. Plz update the question with what you are actually using. The answers given already are all based on doing it in Javascript (since that's the tag you selected) – MrKickkiller Jul 30 '17 at 14:57
  • "my addon" — addon for what? Are you building a Firefox extension? Something else? – Quentin Jul 30 '17 at 15:02
  • @Quentin , I'm expecting something Python related tbh. `json.loads` is a python function. Yet op mentions only Javascript and Json as tags. – MrKickkiller Jul 30 '17 at 15:04
  • @Quentin I already changed the tag for Python, what I intended was for this code to open the link and then open the information that is inside the link – Acruz Jul 30 '17 at 15:11

2 Answers2

0

Python Answer

You can use

import urllib2
urllib2.openurl('https://tugarepo.000webhostapp.com/lib/lib.json').read()

in Python 2.7 to perform a simple GET request on your file. I think you're confusing openfile, which is for local files only and a HTTP get request which is for hosted content. The result of the read() you can put into any JSON library available for your project.

Original Answer for Javascript tag

In plain Javascript, you can use a function like explained in the following: HTTP GET request in JavaScript?

If you're using Bootstrap or Jquery, you can use the following: http://api.jquery.com/jquery.getjson/

If you wanna see the content on the html page (associated with your Javascript), you'll simply have to grab an element from the page (document.getElementById or document.getElementByClass and such). Once you have a DOM element you can add html into it yourself, that contains your JSON data.

Example code: https://codepen.io/MrKickkiller/pen/prgVLe

The above code is based on having JQuery linked in your html Element. There is however an error since your link doesn't have Acces Control headers. Therefor currently only requests coming from the tugarepo.000webhostapp.com domain have access to the JSON file. Consider adding CORS Headers. https://enable-cors.org/

MrKickkiller
  • 501
  • 10
  • 22
-1

Simply do:

fetch('https://tugarepo.000webhostapp.com/lib/lib.json')
  .then(function (response) { return response.json() })
  .then(function (body) { console.log(body)});

But this throws an error as your JSON is invalid.

Thomas Wagenaar
  • 6,489
  • 5
  • 30
  • 73
  • @Acruz, the json is indeed invalid. Try using a tool as https://jsonlint.com/ or https://jsonformatter.curiousconcept.com/ to verify your json to be valid. Invalid json will cause your json.loads call to break. – MrKickkiller Jul 30 '17 at 15:01
  • I'd like some explanation on why my answer got downvoted ?? – Thomas Wagenaar Jul 30 '17 at 15:15
  • Happened to mine aswell. I suspect someone with more knowledge on the problem considered both our answers wrong. – MrKickkiller Jul 30 '17 at 15:16
  • @MrKickkiller I already changed the tag for Python, what I intended was for this code to open the link and then open the information that is inside the link – Acruz Jul 30 '17 at 15:19
  • @ThomasW, not 100% sure, but I don't think he even can. Would be pretty odd that a user with fairly low reputation can downvote answers. Downvoting is 125 reputation minimum – MrKickkiller Jul 30 '17 at 15:31