Log in

No account? Create an account
JSON is good for something other than replacing XML! - Lindsey Kuper [entries|archive|friends|userinfo]
Lindsey Kuper

[ website | composition.al ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

JSON is good for something other than replacing XML! [Apr. 16th, 2007|11:17 pm]
Lindsey Kuper

So I had a big ol' generated JavaScript array of things like:

resources["psych_flashcards_8_3_0"] = new Array();
resources["psych_flashcards_8_3_0"]["name"] = "Flashcards: Operant Conditioning";
resources["psych_flashcards_8_3_0"]["url"] = "http://psychology.lindseys-job.com/flashcards/chapter8/section3/yes/this/is/fake/";
resources["psych_flashcards_8_3_0"]["type"] = "Flashcards";
resources["psych_flashcards_8_3_0"]["fileFormat"] = "html";

One of those for every index. 1422 indices. 7110 lines. 643 KB of JavaScript.

lindseykuper: It's a friggin' enormous .js file. There's probably a more efficient way for me to do that associative array
lindseykuper: like, an object or something
jes 5 1 9 9: javascript doesn't actually do much to distinguish between objects and arrays
jes 5 1 9 9: but you should have a look at JSON
lindseykuper: I've used it. I like it.
lindseykuper: yeah, that totally makes sense for this.
jes 5 1 9 9: I think your resources structure can be represented as JSON without changing any of the other javascript

I thought, "No way. No, it can't." But it turns out it totally can. Now I've got an object of things like

"psych_flashcards_8_3_0": {
"name": "Flashcards: Operant Conditioning",
"url": "http://psychology.lindseys-job.com/flashcards/chapter8/section3/yes/this/is/fake/",
"type": "Flashcards",
"fileFormat": "html"

It comes out to 374 KB. 40% less code, and the cool part is that nothing else had to change, because all my old code that used the double array indices still works. How great is that?

Okay, so this isn't rocket science, and a native speaker of JavaScript would probably have done something object-y right from the start instead of mucking about with huge two-dimensional associative arrays. But still, this is neat. I didn't know that objects and arrays were that interchangeable. And I've never heard of anyone using JSON for anything other than replacing XML.


From: (Anonymous)
2007-04-17 03:30 pm (UTC)

Fun with JSON

If you haven't seen the SIMILE project from MIT, check it out.

They make a nice flat file database application with pretty presentation/manipulation tools. Seem pretty useful, if things don't get _too_ big.

You may also discover that Nixon was our second Quaker president after Herbert Hoover... will wonders never cease.

Put it on a Python... you'll enjoy it more:)
(Reply) (Thread)
[User Picture]From: lindseykuper
2007-04-17 07:38 pm (UTC)

Re: Fun with JSON

That's pretty slick. Yeah, we do it this way so we don't have to worry about server-side code.

I've often read that databases are overkill for this few items, and this makes me believe it.
(Reply) (Parent) (Thread)
[User Picture]From: idealisms
2007-04-18 04:39 am (UTC)
Yeah, in JS there are no associative arrays, only Objects and regular Arrays (which are a special subclass of Object). Objects just happen to act like associative arrays. There are a few catches (like if you happen to have a property called toString, you can no longer serialize to a string), but for the most part, it's a decent data format with lots of language support these days.

On the other hand, if you need to support unicode, it wastes a lot of space because you end up with \uXXXX for each character.

Also, if your data needs to be private (i.e., relies on cookies to download), make sure to avoid Arrays.

On a side note, I've found sqlite to be good for small databases.
(Reply) (Thread)
[User Picture]From: lindseykuper
2007-04-18 05:03 pm (UTC)
No associative arrays. So, resources["psych_flashcards_8_3_0"] = new AnyOldObject(); would have made as much sense as resources["psych_flashcards_8_3_0"] = new Array(); anyway?
(Reply) (Parent) (Thread)
[User Picture]From: lindseykuper
2007-04-18 05:06 pm (UTC)
(Reply) (Parent) (Thread)
[User Picture]From: idealisms
2007-04-18 05:58 pm (UTC)
Yeah, as Mihai points out, Array doesn't have anything to do with associative arrays. Here's another article that explains the difference and why you shouldn't mix.

In your above example, AnyOldObject and Array are different. It would be like writing resources["psych_flashcards_8_3_0"] = {'foo': 'bar'} vs writing resources["psych_flashcards_8_3_0"] = ['foo', 'bar'].
(Reply) (Parent) (Thread)