Scilab Home Page | Wiki | Bug Tracker | Forge | Mailing List Archives | Scilab Online Help | File Exchange
ATOMS : Scilab JSON Toolbox details
Please login or create an account
Dear visitors/customers, we are migrating most of Scilab services into a new data center.
Note that login/registration will not be available for the moment.

Scilab JSON Toolbox

A Simple JSON Toolbox for Scilab
(5833 downloads for this version - 6457 downloads for all versions)
Details
Version
0.2.1
Author
Aditya Sengupta
Owner Organization
Indian Institute of Technology Bombay
Maintainer
Aditya Sengupta
Categories
License
Creation Date
October 2, 2012
Source created on
Scilab 5.4.x
Binaries available on
Scilab 5.4.x:
Windows 64-bit Windows 32-bit Linux 64-bit Linux 32-bit MacOSX
Scilab 5.5.x:
Windows 64-bit Windows 32-bit Linux 64-bit Linux 32-bit MacOSX
Install command
--> atomsInstall("json")
Description
            More information available at: http://forge.scilab.org/index.php/p/json/

Introduction
============

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It
is easy for humans to read and write. It is easy for machines to parse and
generate. It is based on a subset of the JavaScript Programming Language,
[Standard ECMA-262 3rd Edition - December 1999](http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf).


The JSON format is often used for serializing and transmitting structured data
over a network connection. It is primarily used to transmit data between a
server and web application, serving as an alternative to XML.

For more information, please read: 

[http://json.org/](http://json.org/)  
[http://en.wikipedia.org/wiki/Json](http://en.wikipedia.org/wiki/Json)

Structures: 
===========

Since JSON supports key-value objects, the parser interprets the key-value JSON
objects as Scilab ``struct`` objects. 

(Type ``help struct`` in Scilab for more information on Scilab structures.)

Consider the following example:

	{
	  "orderID": 12345,
	  "shopperName": "John Smith",
	  "shopperEmail": "johnsmith@example.com",
	  "contents": [
		{
		  "productID": 34,
		  "productName": "SuperWidget",
		  "quantity": 1
		},
		{
		  "productID": 56,
		  "productName": "WonderWidget",
		  "quantity": 3
		}
	  ],
	  "orderCompleted": true
	}


Assuming the above JSON text is saved in a text file in the current working
directory, the text can be loaded into Scilab and parsed thus: 

    json_eg = mgetl("eg_1.json");
    mystruct = JSONParse(json_eg)

This returns the following Scilab structure:

    mystruct  =
 
    orderID: 12345
    shopperName: "John Smith"
    shopperEmail: "johnsmith@example.com"
    contents: [1x2 struct]
    orderCompleted: "%T"

Individual elements in the structure can be accessed thus: 

Example 1:

    -->mystruct.orderID
     ans  =
 
        12345.

Example 2: 

	-->mystruct.contents.productName
	 ans  =
	 
	 
		   ans(1)
	 
	 SuperWidget   
	 
		   ans(2)
	 
	 WonderWidget   

Note that the JSON values of ``true``, ``false`` and ``null`` are represented in
Scilab as ``%T``, ``%F`` and ``%Nan``. Since Scilab contains a function called
``null()``, the function will be temporarily redefined by the parser. However,
the parser will restore the function after it is done parsing. 

A Note About Matrices
=====================

A special aspect about this particular parser is the way it interprets vectors
and matrices: 

JSON of the form:

    "[1, 2, 3, 4, 5]"

is returned as a (row) vector is Scilab, as one might expect. 

JSON of the form:

    "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"

is returned as a two-dimensional matrix in Scilab: 

    1.    2.    3.  
    4.    5.    6.  
    7.    8.    9.  

as one might expect, since the above JSON is fundamentally an example of a set
of arrays within an array. There is currently no support for hypermatrices. If
you need them, please add an [issue](http://forge.scilab.org/index.php/p/json/issues/create/)
and I'll consider adding support. 

Proceeding with the above convention, a Scilab column vector would be defined in
JSON thus: 

    "[[1], [2],  [3], [4]]"

which when parsed, returns this: 

    1.  
    2.  
    3.  
    4. 

Other Notes
===========

* The parser takes into consideration JSON backslash-escapes. Except for the
backslash-quote, all other backslash-escapes are left as is as they do not
affect Scilab strings. Backslash-quotes are taken into consideration during the
parsing process. 
* This version only has a parser. The next major version will include a JSON
encoder as well. Suggestions on this are welcome. 

            
Files (3)
[48.90 kB]
Source code archive
makes it available for Scilab 5.4
[72.76 kB]
OS-independent binary for Scilab 5.4.x
Binary version
Automatically generated by the ATOMS compilation chain

[12.93 kB]
OS-independent binary for Scilab 5.5.x
Binary version (all platforms)
Automatically generated by the ATOMS compilation chain

News (0)
Comments (1)     Leave a comment 
Comment from Steer Serge -- November 19, 2014, 12:13:26 PM    
It should be useful to add a function to convert scilab data to json
for example the Scilab two-dimensional matrix
    1.    2.    3.  
    4.    5.    6.  
    7.    8.    9.  

my produve JSON of the form:

    "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
Leave a comment
You must register and log in before leaving a comment.
Email notifications
Send me email when this toolbox has changes, new files or a new release.
You must register and log in before setting up notifications.