Version Ref: 6.1 (USP 6.1.01)
filePro now has the ability to import and export JSON files using the JSON
command.
Use these commands to create a JSON file using Processing.
JSON [id] :CR fname
Creates a JSON file with fname
as the filename. The id is optional and defaults to "0" if only one file is open at a
time. If two or more are open, the id must be supplied ("0"-"99").
JSON [id] :CR-|:CL
Closes an open JSON file.
JSON [id] :OB [name]
Starts an object in a JSON file with
name
as the key.
JSON [id] :OB-
Closes an object.
JSON [id] :AR [name]
Starts an array in a JSON file with
name
as the key.
JSON [id] :AR-
Closes an array in a JSON file.
JSON [id] :IT name [value]
Adds an item to a JSON file. If a
value is not supplied, the resulting value will be null
.
JSON [id] :NO name [value]
Adds a number to a JSON file. If a
value is not supplied, the resulting value will be null
.
JSON [id] :BL name [value]
Adds a boolean value to a JSON file.
If a value is not supplied, the resulting value will be null
.
NOTE: JSON files are created by appending one item at a time. When an array or object is started, any items added afterward will be added as direct "children" of the array or object until that array or object is closed.
Processing:
Then: JSON :CR "/tmp/myfile.json" Then: JSON :OB Then: JSON :OB "name" Then: JSON :IT "first" "Tom" Then: JSON :IT "last" "Anderson" Then: JSON :OB- Then: JSON :NO "age" "37" Then: JSON :AR "children" Then: JSON :IT "" "Sara" Then: JSON :IT "" "Alex" Then: JSON :IT "" "Jack" Then: JSON :AR- Then: JSON :IT "fav.movie" "Deer Hunter" Then: JSON :OB- Then: JSON :CL
Output (/tmp/myfile.json
):
{ "name": { "first": "Tom", "last": "Anderson" }, "age": 37, "children": ["Sara", "Alex", "Jack"], "fav.movie": "Deer Hunter" }
Use these commands to read data from a JSON file.
JSON [id] :RO fname
Opens a JSON file for reading. The id is
optional and defaults to "-" if only one file is open at a time. If two or more are
open, the id must be supplied ("0"-"99").
value = JSON [id] :GV key
Get a value from a JSON file using a
path to a key (see Key Syntax below).
Keys are a way to reference part of a JSON document using dot syntax. An example of dot
syntax would be a key, such as "name.first"
or "age"
. There are
reserved symbols used in key syntax that can be used to retrieve certain values from the
JSON:
'#'
is used to get the number of elements inside of an object or
array.
'@'
is used to specify a literal, or if at the end of the path, get
the name of the current object.
Index positions can also be used to reference specific elements by numeric position inside
of an object or an array. Indexes in Key Syntax start at position 1.
x = JSON :GV "fruits.10"
will attempt to find the tenth (10) item inside a
fruits
object or array.
x = JSON :GV "fruits.@10"
will attempt to find a key named "10" inside a
fruits
object and return its value.
Input (/tmp/myfile.json
):
{ "name": { "first": "Tom", "last": "Anderson" }, "age": 37, "children": ["Sara", "Alex", "Jack"], "fav.movie": "Deer Hunter" }
Processing:
Then: JSON :RO "/tmp/myfile.json" ' open the JSON file for reading Then: x=JSON :GV "name.first" ' x contains "Tom" Then: x=JSON :GV "name.1.@" ' x contains "first" Then: x=JSON :GV "age" ' x contains "37" Then: x=JSON :GV "children.#" ' x contains "3" Then: x=JSON :GV "children.1" ' x contains "Sara" Then: x=JSON :GV "fav\.movie" ' x contains "Deer Hunter" Then: JSON :CL ' close the JSON file
NOTE: If a JSON file is missing, malformed, or broken upon attempting to open it, the
file will not be opened and HTMLERRNO()
will
contain a non-zero number:
1 ------- - - - - - - - - - - - - - - - - · If: · Then: JSON :RO "/tmp/mybrokenfile.json" · 2 ------- - - - - - - - - - - - - - - - - · If: HTMLERRNO() ne "0" · Then: errorbox "Something is wrong with your JSON file!"; exit · 3 ------- - - - - - - - - - - - - - - - - · If: ' If no errors, we're okay to get values from the JSON · Then: xx = JSON :GV "name.first"; msgbox "Hi," < xx · 4 ------- - - - - - - - - - - - - - - - -