To obtain JSON data from the Javascript file

advertisements

I have a subject.js javascript file in which json data is stored:

Subject.js Example:

Teacher = {
    "Eng" : "English",
    "Math" : "Maths",
    "Sci" : "Science",
    "SST" : "Social Studies",
    "Hin" : "Hindi"
};

I want to read json data from this js file into another js file.

But I learnt that $.getJSON('filename.json', function(data) { is used to get data from json file but my json data is stored in js file. Can anyone tell me how to get started? I tried using getJson but can't get it to work.


There are two possibilities:

  1. The file can just be included and use the teacher variable as if you ran the getJSON function.
  2. The Teacher = and semi-colon at the end can be removed (verify it is a correct JSON structure) and save the file as .json. Assuming your server will send a JSON file, this should allow the file to be requested.