Functions for working with JSON strings.
Usage of this class to manipulate hierarchical JSON objects is
a bit different from the way that most of the other functions in
the expression language are used.
The main function provided by this class is jsonObject
,
which can be applied to a string value containing a JSON object
(legal JSON object strings are enclosed in curly brackets), and
returns a JSONObject instance.
This JSONObject cannot be used on its own in the rest of the application,
but various methods can be applied to it to extract information
from its structure.
These methods are applied by writing
jsonObject.method(arg,arg,...)
rather than
function(jsonObject,arg,arg,...)
.
Methods you can apply to a JSONObject include:
getString(key)
getInt(key)
getDouble(key)
getBoolean(key)
getJSONObject(key)
getJSONArray(key)
where key
is a string giving the name with which
the member of the JSON object is associated.
The first four of the above methods give you string, numeric, or
boolean values that you can use in the application for plotting etc.
getJSONObject
returns another JSONObject that you can
interrogate with further methods.
getJSONArray
gives you a JSONArray.
You can apply a different set of methods to a JSONArray, including:
getString(index)
getInt(index)
getDouble(index)
getBoolean(index)
getJSONObject(index)
getJSONArray(index)
where index
is an integer giving the (zero-based)
index of the element in the array that you want.
Using these methods you can drill down into the hierarchical structure
of a JSON string to retrieve the string, numeric or boolean values
that you need.
If you are not familiar with this syntax, an example is the
best way to illustrate it.
Consider the following JSON string, which may be the value in a
column named txt
:
{ "sequence": 23, "temperature": { "value": 278.5, "units": "Kelvin" }, "operational": true, "readings": [12, null, 23.2, 441, 0] }To obtain the sequence value, you can write:
jsonObject(txt).getInt("sequence")to obtain the numeric temperature value, you can write:
jsonObject(txt).getJSONObject("temperature").getDouble("value")and to obtain the first element of the readings array, you can write:
jsonObject(txt).getJSONArray("readings").getDouble(0)
Other methods are available on JSONObject and JSONArray;
you can currently find documentation on them at
https://stleary.github.io/JSON-java/.
Note in particular that each of the JSONObject.get*(key)
and JSONArray.get*(index)
methods is accompanied by
a corresponding opt*
method; where the key/index may
not exist, this will probably give effectively the same behaviour
(generating a blank result) but may be considerably faster.
This class also contains some other utility functions for working with JSONObjects and JSONArrays; see the function documentation below for details.
Note: This class is somewhat experimental, and the functions and methods may change in future. An attempt will be made to retain the functions and methods described in this section, but those described in the external JSON-java javadocs may be subject to change.
jsonObject( txt )
The JSON parsing is currently somewhat lenient, for instance allowing a comma before a closing curly bracket.
txt
(String): string assumed to contain a JSON objectjsonArray( txt )
The JSON parsing is currently somewhat lenient, for instance allowing a comma before a closing square bracket.
txt
(String): string assumed to contain a JSON arrayjsonObjectOpt( txt )
For most purposes this will behave the same as the
jsonObject
function,
but it may be slower if there are many invalid or empty JSON strings.
txt
(String): JSON object textjsonArrayOpt( txt )
txt
(String): JSON object textjsonToDoubles( jsonArray )
jsonArray
(JSONArray): JSON ArrayjsonToStrings( jsonArray )
jsonArray
(JSONArray): JSON ArrayjsonGetKeys( jsonObject )
jsonObject
(JSONObject): JSON object