util.JSONObject.insert() adds an element to the JSON object (= a key:value pair).
Syntax
CALL util.JSONObject.insert("key", value)
Parameters
key |
the key part of a new key:value pair |
value |
the value part of a new key:value pair |
Usage and examples
The key:value pair added by util.JSONObject.insert() must be assigned to a variable of the util.JSONObject datatype.
The value of the key:value pair can be a simple string or numeric value as well as a complex value of RECORD or DYNAMIC ARRAY datatype.
MAIN
DEFINE json_obj util.JSONObject
LET json_obj=util.JSONObject.create()
CALL json_obj.insert("simple_string","Test string")
DISPLAY json_obj.toString()
CALL fgl_getkey()
END MAIN
MAIN
DEFINE json_obj util.JSONObject
LET json_obj=util.JSONObject.create()
CALL json_obj.insert("simple_numeric",12345)
DISPLAY json_obj.toString()
CALL fgl_getkey()
END MAIN
MAIN
DEFINE json_obj util.JSONObject
DEFINE cust_rec
RECORD
cust_id INTEGER,
cust_name STRING
END
RECORD
LET json_obj=util.JSONObject.create()
LET cust_rec.cust_id=15
LET cust_rec.cust_name="Barton, Timothy"
CALL json_obj.insert("record",cust_rec)
DISPLAY json_obj.toString()
CALL fgl_getkey()
END MAIN
MAIN
DEFINE json_obj util.JSONObject
DEFINE cust_arr DYNAMIC ARRAY OF INTEGER
LET json_obj=util.JSONObject.create()
LET cust_arr[1] =1
LET cust_arr[2] =12
LET cust_arr[3] =123
CALL json_obj.insert("array",cust_arr)
DISPLAY json_obj.toString()
CALL fgl_getkey()
END MAIN
If a JSON object has a key:value pair of the specified key already, the value of this pair will be changed:
A key:value pair can be returned with util.JSONObject.get().
A key:value pair can be removed with util.JSONObject.remove().