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

May 26, 2011 at 9:26pm

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

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!

Attachments:
  1. JSONreadwrite.zip
#57316
May 26, 2011 at 10:51pm

Check out the jsextensions file which can hold functions you can call from any other javascript you want.

#205333
May 27, 2011 at 12:45am

Oh, hang, that’s too easy! Thanks for the tip.
Peter

#205334
May 27, 2011 at 3:47am

Json is great. I have done a similar thing here http://cycling74.com/forums/topic.php?id=33596

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

http://www.olilarkin.co.uk

#205335
May 30, 2011 at 1:01pm

Thanks for posting this, it’s really helped me out.

#205336
May 30, 2011 at 5:53pm

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.

#205337
May 30, 2011 at 9:47pm

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?

#205338
May 31, 2011 at 7:55am

I think you can set the end of file:

var fout = new File(path, "write", "TEXT");
fout.eof = 0;
// ... write your stuff
#205339
May 31, 2011 at 7:55am

I think you can set the end of file:

var fout = new File(path, "write", "TEXT");
fout.eof = 0;
// ... write your stuff
#205400
May 31, 2011 at 6:21pm

Glad you caught that. I think it’s
fout.eof = 0

#205340
May 31, 2011 at 6:21pm

Glad you caught that. I think it’s
fout.eof = 0

#205404
May 31, 2011 at 10:36pm

Of course Peter. Thanks for mentioning it, I fixed my previous post.

#205341
May 31, 2011 at 10:36pm

Of course Peter. Thanks for mentioning it, I fixed my previous post.

#205405
Jun 1, 2011 at 5:09pm

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

#205342
Jun 1, 2011 at 5:09pm

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

Attachments:
  1. JSONreadwrite.zip
#205408
Jun 12, 2013 at 7:14am

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.

#252514
Jun 24, 2013 at 3:15pm

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!

#253929

You must be logged in to reply to this topic.