05-07-2018 15:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-07-2018 15:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I've got my companion fetching data from various web services with returned json strings. Working great. Today I tapped into another web service json feed that doesn't simply return a json string but rather returns an "output.json" file that I have to save to my file system... Inside that file is... the json string. How to I interact with that json file? I've looked over the fitbit filesystem api stuff but at this point don't know how to save that json file then reopen it and process the json contents.
Thanks much.
Answered! Go to the Best Answer.

Accepted Solutions
05-08-2018 05:42 - edited 05-08-2018 05:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-08-2018 05:42 - edited 05-08-2018 05:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hope this helps.
In your javascript...
import * as fs from 'fs';
Then to open the file
let jsonObject = fs.readFileSync('./path/to/file.json', 'json');
Then you will have perpoerties for each of name / value pairs and arrays ( if they exist ).

05-08-2018 05:42 - edited 05-08-2018 05:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-08-2018 05:42 - edited 05-08-2018 05:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hope this helps.
In your javascript...
import * as fs from 'fs';
Then to open the file
let jsonObject = fs.readFileSync('./path/to/file.json', 'json');
Then you will have perpoerties for each of name / value pairs and arrays ( if they exist ).

05-08-2018 08:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-08-2018 08:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Something else that might be helpful. I have a function that I use to list all the properties of an object and there values.
You could use it to evaluate the load json file.
/** @function listProperties
* Lists all of the properties for a given object.
*/
export function listProperties(object) {
for(var key in object) {
try {
console.log('Key: ' + key + ' | value: ' + object[key]);
} catch (error) {
// Some values throw an error when trying to access them.
console.log('Key: ' + key + ' | Error: ' + error.message);
}
}
}
11-25-2018 17:02
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

11-25-2018 17:02
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Doesn't tell me at all how to print my fitbit data graphs. No help to me.

12-06-2018 12:03 - edited 12-06-2018 12:03
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-06-2018 12:03 - edited 12-06-2018 12:03
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
listProperties - I needed this exact function just now for my project, so thanks for sharing! Works perfectly.

12-06-2018 12:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

12-06-2018 12:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Glad it could help.

01-05-2019 12:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-05-2019 12:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hey kind of a noob question here.
I really want to use your solution but I'm receiving the following javascript error when I include the
Import * as fs from fs;
I'm getting "Uncaught SyntaxError: Unexpected token *". What am I missing? I'm doing this in vanilla javascript if that matters.

01-05-2019 12:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-05-2019 12:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Yeah, I never had much joy with that syntax/style of importing either. If you don't need every single thing in the fs class, then it's a bit wasteful anyway (I would assume). I just use
import { listDirSync, readFileSync, writeFileSync } from 'fs';
and then there's no need to use the "fs." bit at the start of each function, so you save some typing also 😉
e.g.:
writeFileSync(filename, blankWeek, "cbor");
var data = readFileSync(filename, "cbor");
var listDir = listDirSync("/private/data");
01-06-2019 10:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-06-2019 10:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
If ObSkewer suggestion didn't help. Something I noticed is the "I" in import should be lowercase. Your code has it in uppercase.
