Issue Unable to load local JSON file in a javascript file which is spun up with node.
Details
The question is straight forward but I am not serving the javascript file in a browser. I have a javascript file which has the logic, I then spin it up with:
node main.js
I googled a few solutions, they remend using JQuery or XMLHttpRequest but they appear to be running into issues related to the fact i am no serving this in a browser.
Project Background
I am using a raspberry PI to get data from an IR Temperature Sensor. I am using python to get calculate the voltage, convert to celsius, then export that as a JSON file. I then plan to load this file into my javascript file which then configures angular fire Database and pushes this data.
I have a front-end application that will then pull this down and display the end data to the user.
If I go with the JQuery:
Option 1
sample:
$.getJSON("test.json", function(json) {
console.log(json); // this will show the info it in firebug console
});
Error
$.getJSON is not a function even though I am requiring jQuery.
Option 2
If I go with pure javascript, I see
sample
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'my_data.json', true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
Error
xobj.overrideMimeType is not a function
TL/DR
How do I load a local JSON file into a javascript file that is not loaded into a browser but instead spun up with node,
node main.js
Issue Unable to load local JSON file in a javascript file which is spun up with node.
Details
The question is straight forward but I am not serving the javascript file in a browser. I have a javascript file which has the logic, I then spin it up with:
node main.js
I googled a few solutions, they remend using JQuery or XMLHttpRequest but they appear to be running into issues related to the fact i am no serving this in a browser.
Project Background
I am using a raspberry PI to get data from an IR Temperature Sensor. I am using python to get calculate the voltage, convert to celsius, then export that as a JSON file. I then plan to load this file into my javascript file which then configures angular fire Database and pushes this data.
I have a front-end application that will then pull this down and display the end data to the user.
If I go with the JQuery:
Option 1
sample:
$.getJSON("test.json", function(json) {
console.log(json); // this will show the info it in firebug console
});
Error
$.getJSON is not a function even though I am requiring jQuery.
Option 2
If I go with pure javascript, I see
sample
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'my_data.json', true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
Error
xobj.overrideMimeType is not a function
TL/DR
How do I load a local JSON file into a javascript file that is not loaded into a browser but instead spun up with node,
node main.js
Share
Improve this question
edited Dec 20, 2017 at 4:14
Galen
1,3079 silver badges15 bronze badges
asked Dec 20, 2017 at 3:44
BromoxBromox
7072 gold badges13 silver badges35 bronze badges
2
-
4
const jsonData = require('./test.json');
– Explosion Pills Commented Dec 20, 2017 at 3:45 - what?!! I spent hours looking for this single line of code. *facepalm. Thank you very much – Bromox Commented Dec 20, 2017 at 4:16
1 Answer
Reset to default 7Node.js can load JSON files through require
. See the documentation here: https://nodejs/api/modules.html#modules_all_together
- If filename begins with
'./' or '/' or '../'
- If filename.json is a file, parse filename.json to a JavaScript Object. STOP
Most likely what you want is const json = require('./test.json')
assuming test.json
is in the same directory as the code requiring it. Remember that require
parses the JSON, so json
in the example is a JavaScript Object.