util.JSONObject.put()

 

util.JSONObject.put() adds an element (= a name:value pair)  to the specified JSON object.

 

Syntax:

 

util.JSONObject.put("name", value)

 

Parameters:

 

name

a name part of a new name:value pair

value

a value part of a new name:value pair

 

Usage and examples:

 

The name:value pair added by util.JSONObject.put() must be assigned to a variable of the util.JSONObject data type.

 

The value of the name:value pair can be a simple string or numeric value as well as a complex value of RECORD or DYNAMIC ARRAY data type.

 

example code #1

(with a simple string value)

MAIN

  DEFINE json_obj util.JSONObject

    

    LET json_obj = util.JSONObject.create()

    CALL json_obj.put("simple_string", "Test string")

    DISPLAY json_obj.toString()  

    

  CALL fgl_getkey()

END MAIN

 

obtained results #1

 

example code #2

(with a simple numeric value)

 

MAIN

  DEFINE json_obj util.JSONObject

    

    LET json_obj = util.JSONObject.create()

    CALL json_obj.put("simple_numeric", 12345)

    DISPLAY json_obj.toString()  

    

  CALL fgl_getkey()

END MAIN

 

obtained results #2

 

example code #3

(with a JSON object)

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.put("record", cust_rec)

    

    DISPLAY json_obj.toString()  

    

    CALL fgl_getkey()

END MAIN

 

obtained results #3

 

example code #4

(with a JSONArray)

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.put("array", cust_arr)

    

    DISPLAY json_obj.toString()  

    

    CALL fgl_getkey()

END MAIN

 

obtained results #4

 

 

If a JSON object has a  name:value pair of the specified name already, the value of this pair will be changed:

 

 

A name:value pair can be returned with the util.JSONObject.get() method.

A name:value pair can be removed with the util.JSONObject.remove() method.

 

 

Example programs:

CVS server: client.querix.com

CVS repository: /lycia_doc_examples

User: client

Project: auxiliary_features/json

Program: util_JSONObject_put

 

Related articles:

util.JSONObject.get()

util.JSONObject.remove()