Add$

Add$ inserts data into a JSON document using a JSON pointer.

Syntax

 RETURN_VALUE = JSON_OBJECT.Add$(EXPRESSION_KEY, EXPRESSION_VALUE)
  • RETURN_VALUE is a boolean that indicates whether the operation was successful or not.
  • JSON_OBJECT is an instance constructed using the ParseInstance instruction applied to a JSON document. This corresponds to the JSON parser used to access and modify the JSON document.
  • EXPRESSION_KEY is an expression that corresponds to the key being added to the JSON document. EXPRESSION_KEY uses JSON pointer syntax. You can find more information regarding the syntax of JSON pointers by reading the standard documentation here.
  • EXPRESSION_VALUE is an expression that corresponds to the value being added to the JSON document. It can be a string, a boolean, an integer, an object or an array.

Examples

# Let's add a string to a JSON document (if absent from the JSON document):
Funprog Add_Str()
	 Local Clbfile SRCJSON
        SRCJSON = '{"foo": "bar", "bar": "def"}'
        Local Instance OBJ using Object
        ParseInstance OBJ with SRCJSON
        Local Boolean R1, R2
        R1 = OBJ. Add$("/baz", "qux")
        Local Clbfile MYCLB
        R2 = OBJ.Get$("", MYCLB)
        FreeGroup OBJ
End MYCLB -> Returns {"baz": "qux", "bar": "def", "foo": "abc"}

# Let's add a boolean to a JSON document (if absent from the JSON document):
Funprog Add_Bol()
	 Local Clbfile SRCJSON
        SRCJSON = '{"foo": "abc", "bar": "def"}'
        Local Instance OBJ using Object
        ParseInstance OBJ with SRCJSON
        Local Boolean R1, R2
        R1 = OBJ.Add$("/baz", true)
        Local Cblfile MYCLB
        R2 = OBJ.Get$("", MYCLB)
        FreeGroup OBJ
End MYCLB -> Returns {"baz": true, "bar": "def", "foo": "abc"}
# Let's add an integer to a JSON document (if absent from the JSON document):
Funprog Add_Int()
	 Local Clbfile SRCJSON
        SRCJSON = '{"foo": "abc", "bar": "def"}'
        Local Instance OBJ using Object
        ParseInstance OBJ with SRCJSON
        Local Boolean R1, R2
        R1 = OBJ.Add$("/baz", 42)
        Local Cblfile MYCLB
        R2 = OBJ.Get$("", MYCLB)
        FreeGroup OBJ
End MYCLB -> Returns {"baz": 42, "bar": "def", "foo": "abc"}
# Let's add an object to a JSON document (if absent from the JSON document):
Funprog Add_Obj()
	 Local Clbfile SRCJSON
        Local Instance OBJ using Object
        ParseInstance OBJ with SRCJSON
        Local Boolean R1, R2
        Local Instance NULLOBJ using Object
        R1 = OBJ.Add$("/object", NULLOBJ)
        Local Cblfile MYCLB
        R2 = OBJ.Get$("", MYCLB)
End MYCLB -> Returns {"object": {}}
# Let's add an array to a JSON document (if absent from the JSON document):
Funprog Add_Array()
	 Local Clbfile SRCJSON
	 Local Interface OBJ using Object
        ParseInstance OBJ with SRCJSON
        Local Boolean R1, R2
        Local Integer Colors(0...) -> Empty array
        R1 = OBJ.Add$("/colors", COLORS)
        Local Clbfile MYCLB
        R2 = OBJ.Get$("", MYCLB)
End MYCLB -> Returns {"colors": []}

Description

Add$ adds a key and its value to a JSON document using the JSON pointer syntax.

The result of this function has the boolean type.

Return values

Value Explanation
0 Operation succeeded.
-6 The name of the variable does not exist in the JSON document.
-26 Invalid JSON document or unable to add as the key already exists in the JSON document.
-70 Invalid argument type.

See also

Contains$
Get$
Parse Instance
Remove$
Replace$
Select$