[sharing is fun] example: write and read JSON in javascript

    May 26 2011 | 9:26 pm
    I searched around on the forum for this stuff, and managed to gather the necessary stuff for a complete example. Thanks to Adam Murray for the pointer to the main ingredient! The attached zip file has the js and maxpat that demonstrate how to populate an object in javascript with some stuff (I have it stored in colls, initially) then turn that into a JSON file, then read that JSON file to repopulate your object in the js at a later date. I'm curious if there's a way to break out the actual parsing functions into a separate file that I could include somehow. I would like this parser to be available to several scripts, but I'd rather not cut and paste it into every one!

    • May 26 2011 | 10:51 pm
      Check out the jsextensions file which can hold functions you can call from any other javascript you want.
    • May 27 2011 | 12:45 am
      Oh, hang, that's too easy! Thanks for the tip. Peter
    • May 27 2011 | 3:47 am
      Json is great. I have done a similar thing here https://cycling74.com/forums/sharing-max-web-control-v0-1
      I hope max6's js engine gets updated. Lots of interesting stuff going on with js at the moment (e.g node.js) and it would be cool to be able to #include files using require()
    • May 30 2011 | 1:01 pm
      Thanks for posting this, it's really helped me out.
    • May 30 2011 | 5:53 pm
      You can use eval() to include other javascript files if you want. Although this comes with problem of making sure the file you're including contains no errors because eval() will load it in as-is. Here's an example of some code I load in to jsextensions that will make this easier, go take a look.
    • May 30 2011 | 9:47 pm
      One thing I've noticed with the var fout = new File(path,"write","TEXT");
      line is if my file was once larger than the JSON output is currently, the old extra contents are still at the bottom of the file.
      Is there anyway to zero out the file beforehand?
    • May 31 2011 | 7:55 am
      I think you can set the end of file:
      var fout = new File(path, "write", "TEXT");
      fout.eof = 0;
      // ... write your stuff
    • May 31 2011 | 6:21 pm
      Glad you caught that. I think it'sfout.eof = 0
    • May 31 2011 | 10:36 pm
      Of course Peter. Thanks for mentioning it, I fixed my previous post.
    • Jun 01 2011 | 5:09 pm
      And thanks for posting it. I was actually in the midst of suffering the problem when I happened to check back to this post, and there was the solution! I suppose that is a disadvantage of posting a zip file, as that bug is embedded in eternity up there. Well, for the sake of the archive, here's a fixed version
    • Jun 12 2013 | 2:14 pm
      Thank you for writing this! It has been immensely helpful in a project I've been working on for a while now.
      This morning I was testing saving out very large JS objects (Around 10000 key/value pairs in about 10 levels of depth) and it is truncating the JSON file pretty severely (at around 2000 entries).
      I've started an attempt at debugging this but was hoping someone could offer some insight. Thanks.
    • Jun 24 2013 | 10:15 pm
      Am new to json, I need to create json file for following case.
      In html once am getting input data and need to store tat in json file.
      Am not that much good in java script.
      Can some please guide me how to do.
      Thanks in Advance!
    • Sep 23 2014 | 12:47 pm
      After wasting a whole evening trying to coerce [text] and [jit.text] to save a large jsonified text (they have 1069 char and 2048 char limits respectively), your json_readwrite code worked beautifully -- thanks!