Subscribe Bookmark RSS Feed
XanGregg

Staff

Joined:

Jun 23, 2011

JSON Parsing Functions

This JSL file contains functions for converting JSON text into either a JSL data structure or a JMP data table. JSON represents a structure of nested "arrays" and "objects". JSON types/values are represented as follows in JSL:

    JSON   JSLNotes
arraylistsequence of values
objectassociative arrayset of named values
numbernumber
stringstring
true1.0
false0.0
null.missing value

When converted to a JMP data table, names represent columns and array values represent rows. Non-array values are filled down to match array values.

Main Functions

All functions are within the namespace "JSON" to avoid name collisions with other functions.


dt = JSON:Make Table( json text | obj )


obj = JSON:Parse( json text )



Example Data

Original JSON


    {"region":"Europe",


     "country":[


         {"name":"France", "population":66.1},


         {"name":"Germany", "population":80.8}


       ]


   }



Converted JSL


Associative Array(


   {{"country", {


      ["name" => "France", "population" => 66.1],


      ["name" => "Germany", "population" => 80.8]


   }},


   {"region", "Europe"}}


)



Note the order of the set elements is not preserved. It becomes alphabetical in JMP.

Values of the parsed object can be accessed with normal JSL subscripting. For example, if data holds the above parse result, you can access it with:


   data["country"][1]["name"];      // "France"



Converted Data Table

country.namecountry.populationregion
France66.1Europe
Germany80.8Europe

Note that the region value gets repeated.

Implementation notes

Please report issues to the File Exchange discussion or to xan.gregg@jmp.com.

Update: I've also attached Craige's JSON Parser II.jsl, which is a much much faster version of JSON Parser.jsl.

Comments
Craige_Hales

Clever code Xan, using a JSL list as a stack to deal with the nested JSON structure is a great idea. 

Fast JSON Parsing using Pattern Matching  Faster variation of your code, still not well tested.

Thanks for pointing me at the Insert function, that was an important piece of the puzzle.


XanGregg

Great -- my 5 minute parse is now down to 1 second!

Article Tags