Read the statement by Michael Teeuw here.
Use a local JSON file with MMM-JsonTable Module
-
Hi,
I’m trying to access a JSON file on my MM Raspberry Pi to display the data using MMM-JsonTable.
The config asks for a url - and I’ve tried all sorts to find the correct syntax, all to no avail. The latest effort is shown below but it doesn’t work either.
This is my current config.js entry for the module:
{ module: 'MMM-JsonTable', position: 'top_left', header: 'xxxxxx', config: { url: 'https://localhost:8080/modules/MMM-JsonTable/xxxxxx.json', // Required arrayName: 'xxxx' // Optional } },
I’ve tried loading from another of my Pis after copying the file to the html directory and using the IP address of the Pi plus file name but no joy.
I have pasted the JSON data directly into the module’s MMM-JsonTable.js file and it works fine - I just can’t figure out how to get the data from a local file.
I wondered if it was a permissions thing. The file attributes are
-rw-r--r-- 1 pi pi 4974 Sep 2 12:17 xxxxxx.json
so every man and his dog can read the file.
Thanks in advance for any solution - assuming there is one.
-
Try:
url: 'modules/MMM-JsonTable/xxxxxx.json'
In this case, the xxxxxx.json file must be in the root of the MMM-JsonTable folder. Of course, the module has to support local json files.
-
@Mykle1
Thanks for the reply. I had tried that url plus others. I even put the file in a folder within the module folder named ‘public’ but that didn’t work either.I don’t think the module supports local json files - how do I determine this?
I think I’ll try to develop my own module - it’ll be a good learning experience if nothing else.
Thanks again.
-
@reb
You can check Which url is derived from youconfig.js
by this;
node_helper.js, in line 11... getJson: function (url) { var self = this; console.log(url) // add this. request({ url: url, method: 'GET' }, function (error, response, body) { ...
-
OK, I have solved the issue - the json file causes this error:
|mm | Whoops! There was an uncaught exception... 0|mm | SyntaxError: Unexpected token in JSON at position 0 0|mm | at JSON.parse (<anonymous>) 0|mm | at Request._callback (/home/pi/MagicMirror/modules/MMM-JsonTable/node_helper.js:15:21) 0|mm | at Request.self.callback (/home/pi/MagicMirror/node_modules/request/request.js:186:22) 0|mm | at emitTwo (events.js:126:13) 0|mm | at Request.emit (events.js:214:7) 0|mm | at Request.<anonymous> (/home/pi/MagicMirror/node_modules/request/request.js:1163:10) 0|mm | at emitOne (events.js:116:13) 0|mm | at Request.emit (events.js:211:7) 0|mm | at IncomingMessage.<anonymous> (/home/pi/MagicMirror/node_modules/request/request.js:1085:12) 0|mm | at Object.onceWrapper (events.js:313:30) 0|mm | MagicMirror will not quit, but it might be a good idea to check why this happened. Maybe no internet connection? 0|mm | If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues
There are three bytes at the beginning of the json file before the first ‘{’. I am using an Excel to JSON converter from where I copy and paste the data.
I use VS Code as my code editor and with a blank new file, when I paste the data from the online converter and save, those extra bytes are at the start of the file. Same with Notepad and Editplus.
Without these 3 extra bytes, the module loads the file OK. Now I just need to figure out how to save the data without those extra bytes.
BTW, the correct url that I used is:
'http://192.168.1.27:8080/modules/MMM-JsonTable/xxxxxx.json'
-
OK, I’ve gotten to the bottom of the 3 extra bytes at the beginning of my json file - they are the utf-8 BOM (EF BB BF).
Fortunately, I have a script (on a Directory Opus button) that will remove these bytes (or put them back if need be).
Thanks to those that offered advice etc.
-
@reb those characters are called the BOM (Byte Order Mark)… added by advanced word processing editors…
u can use Notepad++ to remove them too…
u need to change editors to make sure you don’t add them back accidentally…
-
@sdetweil said in Use a local JSON file with MMM-JsonTable Module:
@reb those characters are called the BOM (Bill of Material)… added by advanced word processing editors…
u can use Notepad++ to remove them too…
u need to change editors to make sure you don’t add them back accidentally…
@sdetweil
Thanks for the info about removing the BOM (Byte Order Mark). I use an Explorer replacement program called Directory Opus which is awesome and one of the developers wrote a script that can be attached to a toolbar button that can add, remove or toggle the Byte Order Mark.