Load and parse locally saved JSON files

I can succesfully download a JSON file from a remote server and save it into dataDirectory. As I would like to be able to access its informations oflline too, I thought it could be easily been parsed like this:

var json = FileSystem.readTextFromFileSync(FileSystem.dataDirectory + "/myfile.json");

The above code does actually read the JSON file into a string as confirmed by console.log(json). Then I though I had to parse it like this:

var myjson= JSON.parse(json);

And access its values like this:

console.log(tsn.info.num_events);

But nothing is returned. My JSON file is kind of the following:

{"tsn": {
    "info": 
        {"num_events": "98",  "version": "1.0"}, 
    "occurrences": 
        ["Party", "Music"]
    }
};

Maybe I’m wrong and a JSON file cannot be loaded using FileSystem.readTextFromFile? How should be done instead?

EDIT. Searching through the forum I found a post that seems making it really easy, but I still can’t parse the file. Suppose the above JSON structure, I get Fuse.Scripting.V8.Object:

var json = JSON.parse(Storage.readSync("/tsn.json"));
for (var prop in json) {
    console.log(json[prop].info.num_events);
    console.log(json[prop].occurrences);
}

Hi!

Your approach should work. Are you sure your JSON data is actually correct?
; for example (as is in your snippet) is not allowed in JSON.

Hi Kristian,

(The ; it’s a typo.)

I don’t know if this is actually how Fuse is expected to work or I’m wrong somewhere, but I noticed the following.

After a number of tests I discovered that to parse a Json file the file must be saved using Storage and not using .dataDirectory, where in Windows "Storage" = ..\AppData\Local\ and ".dataDirectory" = [Fuse_project]\build\Local\Preview\fs_data\.

This means that the following code will output correctly because the json file has been saved using Storage (without the slash /):

var json = JSON.parse(Storage.readSync("tsn.json"));
for (var prop in json) {
    console.log(json[prop].info.num_events);
}

On the other hand this code will not work and it’s going to return Cannot read property 'num_events' of undefined:

var json = FileSystem.readTextFromFileSync(FileSystem.dataDirectory + "/tsn.json");
for (var prop in json) {
    console.log(json[prop].info.num_events);
}

In your opinion does this makes sense? A part from the above reason I did not understand what’s the meaning using Storage rather than .dataDirectory or viceversa. Why there are two places where to save data?

[Fuse version 0.34.0, build 10613]

Hi!

No this shouldn’t matter. When you say “Storage vs .dataDirectory”, do you actually mean our Storage module vs our FileSystem module?
https://www.fusetools.com/docs/fuse/storage/storagemodule vs https://www.fusetools.com/docs/fuse/filesystem/filesystemmodule.

In that case, it makes no difference, but you should always prefer to use FileSystem over Storage.

What you have to rember though, is that none of these APIs parse the JSON for you. They simple write and read text to a file.

So, if you have some data that you want to write, you have to turn it into a string before you write it:

var data = { some: "object" };
var json_data = JSON.stringify(data);
FileSystem.writeTextToFileSync("theFile.json", json_data);

and then when you read it:

var json = FileSystem.readTextFromFileSync(FileSystem.dataDirectory + "/tsn.json");
// ^^ this is just text at this point
var json_parsed = JSON.parse(json);
// ^^ now we have an actual object and the next part should work
for (var prop in json_parsed) {
    console.log(json_parsed[prop].info.num_events);
}

I hope this clarified things for you :slight_smile:

Oook! Got it. Thank you very much.