Load data from json file
i have a really little problem and i think it is easy to be solved. I have created a new module where i need to load a local json file which is created by an external program.
I cant figure out how i can read this file and where to locate that one. I have created a variable with the filetext so i know i can handle it if i read it. But i cant get it to read the file so i can remove the variable because the data changes every week.
i hope someone can help me. Sorry if im wrong here but i dont know how i can help myself
is there really noone who wants to help me?
I’m not exactly in the same boat, but similar I guess… Maybe we can put our heads together or someone more knowledgeable will notice the thread…
I’ve got a local weather station that I have putting plain text data (for temperature) on the SD card of MagicMirror . My goal is to put a ‘realtime’ (updated every minute or so) temperature on the display, directly from my own weather station.
As an initial step to test this out, I went into
currentweather.jsand changed the following in the processWeather function:
this.temperature = this.roundValue(data.main.temp);
var fs = new require('fs'); this.temperature = fs.readFileSync('/var/www/html/wx/temp.txt');
I anticipated that this would put the contents of my file in place of the temperature data from openweathermap. Unfortunately, it results in the currentweather module sticking at
Just to check I’m not insane, I replaced my code with:
this.temperature = '56.0';
The module loads with 56.0 as the temperature. This leads me to believe I’m having an issue reading the file from disk. I threw together a quick js file to test I’m doing the
var sys = require("sys"); var fs = new require('fs'); var temp = fs.readFileSync('/var/www/html/wx/temp.txt'); sys.puts(temp);
Low and behold, my file contents show up just fine.
As far as I can tell, this SHOULD work, but it clearly isn’t. Maybe you and I are running into similar issues reading from file? Anyone out there that can assist us?
A simple Google search for
node parse json filereveals this
For a solution without node another Google search for
I hope this helps!
Please also make sure you use Markdown on code snippets so it is easier to read!
@paviro Difference on mine is I’m pulling in a straight value from a text file, not JSON. The file is a text file that has current temperature. No markups tags or anything else.
I’m no expert at Java so maybe I’m missing something?
@mattlugar not sure how
currentweather.jsdeals with data it’s @MichMich’s module he might know something. But it would probably be easier to have a look at the module development README and write a simple module for your purpose from scratch.
@paviro Ah, well thanks for the feedback. I’m hoping to get a bit of a hybrid between that app and pulling data from my own weather station. I’d like to keep the sunrise/sunset and the current conditions coming in from openweathermap and put my own station’s temp/wind. Was thinking modifying his would be a little easier than from scratch. Maybe I’ll get lucky and he’ll see this and point me in the right direction.