How to write data into json files in alljoynjs?

asked 2015-04-08 00:11:05 -0700

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.


I am trying to write json objects data to some json file. I tried something like

AJ.store(“mySavedState”, myState);

as mentioned at this link https://wiki.allseenalliance.org/_media/training/programming_alljoyn.js.pdf page number 28.

Everything is fine as you mentioned at the link. Since, I am using alljoynjs on raspberry It is using NVRAM to hold back the json objects in the code. But, I am expecting json objects data in json files.

Something similar to be used in node js like this

var fs = require('fs');

var mydata = { "name" : "AllJoynJS" };

fs.writeFile("my.json", JSON.stringify(mydata, null, 4), function(err) {
    if(err) {
     } else {
         console.log("JSON saved to my.json");

This code can write my data to json file. I am expecting this kind of code with alljoyn. Can I have this kind of code with alljoyn?

And, Let me know "Is there any possibility to import modules in alljoyn something like var fs = require('fs');? "

Thanks, Ganesh

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2015-04-08 09:08:31 -0700

jprestwo gravatar image

updated 2015-04-08 09:22:10 -0700

AllJoyn.js uses the same NVRAM files as AJTCL. You cannot change this file type to a json file. Any persistent storage created with AJ.store() will go into this single file, ajtcl.nvram. The AJ.store() function was intended only for use within AllJoyn.js and not really for external use, since it uses its own format to write the data to the file.

AllJoyn.js has no correlation to node meaning you can't use require() for any library you can with node. The only libraries that you have access to are AllJoyn and IO.

If you must create a json file you could do something with the IO.system() command. You provide a string to IO.system() and it executes it as if its in a bash script/command line (it directly maps to the standard library 'system()' command on linux). You could do something like:

 IO.system('echo "' + <JSON string> + '" > file.json');.

I think this would achive what your trying to do.

edit flag offensive delete publish link more


Thanks. It can help me.

kgan4dev ( 2015-04-08 22:02:19 -0700 )edit

By the way we are always interested in others committing code to all the AllJoyn projects. If you want to contribute a 'fs' module it would be appreciated and then it would be much easier to do what you need (less of a hack at least)

jprestwo ( 2015-04-09 11:30:45 -0700 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-04-08 00:11:05 -0700

Seen: 2,304 times

Last updated: Apr 08 '15