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 | JSL | Notes |
---|---|---|
array | list | sequence of values |
object | associative array | set of named values |
number | number | |
string | string | |
true | 1.0 | |
false | 0.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.
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 )
{"region":"Europe",
"country":[
{"name":"France", "population":66.1},
{"name":"Germany", "population":80.8}
]
}
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"
country.name | country.population | region |
---|---|---|
France | 66.1 | Europe |
Germany | 80.8 | Europe |
Note that the region value gets repeated.
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.
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.
Great -- my 5 minute parse is now down to 1 second!