Forum OpenACS Development: Re: JSON parser for TCL/NaviServer

Collapse
Posted by Neophytos Demetriou on
Greetings to all,

We are getting close to releasing the first version of tjson. I have added JSON manipulation commands very much like what tDOM does with XML. Please note that I had to change the command names in my original post to json_to_simple, json_to_typed, and typed_to_json to accommodate the manipulation commands.

Here is a quick example:

package require tjson

set node_handle [::tjson::parse { { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": "8.95" }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": "12.99" }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": "8.99" }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": "22.99" } ], "bicycle": { "color": "red", "price": "19.95" } } }}]

set store_handle [::tjson::get_object_item $node_handle store] ::tjson::add_item_to_object $store_handle car [list M \ [list brand {S "Mercedes"} color {S blue} price {N 19876.57}]]

set book_handle [::tjson::get_object_item $store_handle book] ::tjson::add_item_to_array $book_handle [list M \ [list \ category {S "fiction"} \ author {S "Fyodor Dostoevsky"} \ title {S "Brothers Karamazov"} \ isbn {S "0679410031"} \ price {N "22.19"}]]

puts [::tjson::to_pretty_json $node_handle] ::tjson::destroy $node_handle

And here is the output:

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": "8.95"
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": "12.99"
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": "8.99"
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": "22.99"
      },
      {
        "category": "fiction",
        "author": "Fyodor Dostoevsky",
        "title": "Brothers Karamazov",
        "isbn": "0679410031",
        "price": 22.19
      }
    ],
    "bicycle": {
      "color": "red",
      "price": "19.95"
    },
    "car": {
      "brand": "Mercedes",
      "color": "blue",
      "price": 19876.57
    }
  }
}

The only things pending for the release of the first version are:

  1. Tests
  2. Refactor escape_json_string
  3. Build the library on macOS

Once the release is out, I will try to add JSONPATH support.

As usual, any feedback or pull requests are more than welcome.

PS. I told a friend that I had built an extension for parsing JSON in TCL and the first question he asked was how you were all performing API calls to the backend from the frontend (he comes from the Node.js/React/Typescript world) and the answer is I do not know but my best guess is that you did not make any. I would greatly appreciate it if someone could enlighten me.