Converting JSON data into a list


Post #: 81
Date: 2013-01-30 16:28:47.000
Author: Rocket Man

JSON stands for the JavaScript Object Notation, and it's used by lots of applications because it's much more compact than the bulky XML format. It's also suspiciously similar to LISP lists, which isn't that surprising considering that JavaScript was originally written as a LISP interpreter.

The basic format is something like this:


{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}


I've written a function in Rockets that converts this notation into a native newLISP list. Basically all I've done is replace { with ( and [ with (, then removed colons, commas, and other extraneous syntax. I ran into a small problem with very large text strings, because newLISP's internal list evaluator doesn't like them and returns a "string token too long" error. To fix this, I put [text] and [/text] in place of everything that had quotes, which tells newLISP's internal evaluator to treat it as a long string of text. Using (read-eval) I convert this long string of long strings back into a list, and all the [text] and [/text] goes away and you have a normal list!

The function is called (convert-json-to-list) and it's included in Rockets. I'm already finding it useful for my new project.


xerxes on 2013-01-31 13:05:36.000

Wow. You really are soldiering on - do you ever rest?

This is impressive, but I am curious - did you not find Jeff Ober's json.lsp adequate?

xerxes on 2013-01-31 13:06:21.000

Wow. You really are soldiering on - do you ever rest?

This is impressive, but I am curious - did you not find Jeff Ober's json.lsp adequate?

Rocket Man on 2013-01-31 16:19:32.000

Heh... I rest a bit too much, actually. I have lots of goals.

I tried Jeff's json.lsp, but it had errors when I was trying to parse the JSON output of the Solr database (which was a project I was working on at the time for work). I tried debugging the error, but it was one of those things where it was actually easier and faster to write new code for my purposes rather than try and fix the problem in that code. This particular example was very much a "Gordion Knot" approach, which forced me (due to time constraints) to just ask myself what the simplest possible way of converting JSON to a list would be.

The neat thing about LISP is how easy it is to use someone else's function if it works or rewrite it if it doesn't.


lutz on 2013-02-09 07:02:29.000

newLISP development version 10.4.6, which came out just a few days ago, has JSON parsing built in as a native function. See the documentation here: http://www.newlisp.org/downloads/development/newlisp_manual.html#json-parse

Rocket Man on 2013-02-09 11:12:22.000

Cool! I'll check that out.

View this post in the forums

Views: 3864