Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

json file return, not sure how to process it

ANSWERED

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.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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 ).

View best answer in original post

Best Answer
0 Votes
8 REPLIES 8

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 ).

Best Answer
0 Votes

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);
  }
 }
}

Best Answer

Doesn't tell me at all how to print my fitbit data graphs.  No help to me.

Best Answer
0 Votes

listProperties - I needed this exact function just now for my project, so thanks for sharing! Works perfectly.

Best Answer
0 Votes

Glad it could help.

Best Answer
0 Votes

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.

Best Answer
0 Votes

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");

Best Answer

If ObSkewer suggestion didn't help.  Something I noticed is the "I" in import should be lowercase. Your code has it in uppercase.

Best Answer