JSON Functionality
This page documents the JSON functionality available in noxDB. Header file: headers/JSONPARSER.rpgle
.
- json_SetDelimiters
- json_SetDelimiters2
- json_ParseFile
- json_ParseString
- json_WriteJsonStmf
- json_AsJsonText
- json_Error
- json_Message
- json_Locate
- json_LocateOrCreate
- json_NewObject
- json_MoveObjectInto
- json_GetLength
- json_GetChild
- json_NewArray
- json_ArrayPush
- json_GetStr
- json_SetStr
- json_GetNum
- json_SetNum
- json_GetInt
- json_SetInt
- json_SetBool
- json_GetNext
- json_GetName
- json_IsTrue
- json_SetIterator
- json_ForEach
- json_NodeDelete
- json_Close
json_SetDelimiters
void json_SetDelimiters( String characters )
Function is deprecated, has been replaced with json_SetDelimiters2
.
Parameters
- 5 characters which represent delimiters in a programs characters set. List of characters in their order:
- Forward slash
- Back slash
- Master space (
@
) - Square bracket begin
- Square bracket end
json_SetDelimiters2
void json_SetDelimiters2( String characters )
Parameters
- 11 characters which represent delimiters in a programs characters set. List of characters in their order:
- Forward slash
- Back slash
- Master space (
@
) - Square bracket begin
- Square bracket end
- Blank
- Dot
- Curley bracket begin (
{
) - Curley bracket end (
}
) - Apostrophe (
'
) - Quote / Speech mark (
"
)
Example
json_setDelimiters2('/\@[] .{}''"');
json_ParseFile
Pointer json_ParseFile( String path : [String options] )
Parameters
- Path relavtive to the current directory.
- Options for the parser.
Returns Pointer to created node.
json_ParseString
Pointer json_ParseString (String json : [String options])
Parameters
- JSON document string.
- Options for the parser.
Returns Pointer to created node.
json_WriteJsonStmf
void json_WriteJsonStmf( Pointer node : String filepath : Int(10) ccsid : Ind trim : [String options] )
Parameters
- Pointer to existing node.
- File in which to write the JSON to.
- CCSID of the file (
1208
is always a good option) - Trim down the JSON in the output.
- Options for the generator
json_AsJsonText
Varchar(32767) json_AsJsonText( Pointer node )
Parameters
- Pointer to existing node. Usually the node which is at the top of the document.
json_Error
Ind json_Error( Pointer node )
Parameters
- Pointer to node created by another JSON function.
Returns *ON
if there was an error.
Example
pJson = json_ParseFile('./test/documents/simple.json');
If json_Error(pJson) ;
//Do thing
Endif;
json_Close(pJson);
json_Message
Varchar(1024) json_Message( Pointer node )
Parameters
- Pointer to node created by other another JSON function.
Returns error message create by another JSON function.
json_Locate
Pointer json_Locate( Pointer node : String nodepath )
Parameters
- Pointer to node created by other another JSON function.
- Document path which points to a node.
Returns Pointer to chosen document node.
Example
pJson = Json_ParseFile ('./test/documents/simple.json');
pNode = Json_Locate(pJson: '/price');
price = Json_GetNum(pNode);
{
"price": 1234,
"text" : "System & Method",
"anObject" : {
"country" : "Denmark"
}
}
json_LocateOrCreate
Similar to json_Locate
, but if the node does not exist, it will be created.
Pointer json_LocateOrCreate( Pointer node : String nodepath )
Parameters
- Pointer to node created by other another JSON function.
- Document path which points to a node.
Returns Pointer to chosen document node.
Example
pObj = json_newObject();
pA = json_locateOrCreate(pObj : 'aValue');
pA2 = json_setStr(pA : '' : 'Value for a');
json_NewObject
Pointer json_NewObject( Pointer node )
Parameters
- Destination JSON node. For example, you could pass in the Pointer from
json_Locate
orjson_LocateOrCreate
.
Returns object pointer.
Note: The parameter is now obsolete. It is easy to use this API along side json_MoveObjectInto
:
pO1 = json_NewObject();
pO2 = json_NewObject();
json_MoveObjectInto(pO1 : 'myChild' : pO2)
json_MoveObjectInto
Pointer json_MoveObjectInto( Pointer destinationObject : String objectname : Pointer sourceObject )
Parameters
- Existing destination object node
- New name of object in destination object
- Contents of new object from an existing object node
Example
pObj = json_newObject();
json_setStr(pObj : 'a' : 'Value for a');
pB = json_newObject();
json_setStr(pB : 'bValue' : 'Value for b');
json_MoveObjectInto(pObj : 'bObject': pB);
json_GetLength
Int(10) json_GetLength( Pointer node )
Parameters
- Existing JSON node which is an array.
Returns count of elements in provided array.
json_GetChild
Pointer json_GetChild( Pointer node )
Parameters
- Existing JSON node which is an array or object.
Returns Pointer which is the first item in the array or first node in the object
json_NewArray
Pointer json_NewArray( Pointer node )
Parameters
- Existing JSON node. For example, you could pass in the Pointer from
json_Locate
orjson_LocateOrCreate
.
Returns array pointer.
Note: The parameter is now obsolete. It is easy to use this API along side json_MoveObjectInto
.
json_ArrayPush
Pointer json_ArrayPush( Pointer existingArray : Pointer sourceNode : Uns(5) type )
Parameters
- An existing array. Either locate one or use the pointer from
json_NewArray
. - What existing node to push to the array.
- One of the following constants:
JSON_MOVE_UNLINK
- push the existing node by reference.JSON_COPY_CLONE
- make a duplicate of your node and push the duplicate.
Returns pointer to node in array.
json_GetStr
Varchar(32767) json_GetStr( Pointer node : [String relativeNode] : [String defaultvalue] )
Parameters
- Pointer to existing node
- Relative path to node
- Default value if not node value is found.
json_SetStr
Pointer json_SetStr( Pointer node : [String relativeNode] : [String newvalue] )
Parameters
- Pointer to existing node
- Relative path to node
- New value.
json_GetNum
Packed(30:15) json_GetNum( Pointer node : [String relativeNode] : [Packed(30:15) defaultvalue] )
Parameters
- Pointer to existing node
- Relative path to node
- Default value if not node value is found.
json_SetNum
Pointer json_SetNum( Pointer node : [String relativeNode] : [Packed(30:15) newvalue] )
Parameters
- Pointer to existing node
- Relative path to node
- New value.
json_GetInt
Int(20) json_GetInt( Pointer node : [String relativeNode] : [Int(20) defaultvalue] )
Parameters
- Pointer to existing node
- Relative path to node
- Default value if not node value is found.
Example
{
o: {
e:[],
n:123,
a: [1,2,3,4,5],
m:"yyyy",
f: "john"
},
u: "This is a unicode \u00b5 string" ,
"s":"abc" ,
a:[1,2,3],
x:"xxxxx",
"yo":{a:[],b:1}
}
pJson = Json_ParseFile ('./test/documents/demo.json');
pNode = Json_locate(pJson:'/o/n');
n = json_GetInt(pNode); //123
pNode = Json_locate(pJson:'/a[1]');
n = json_GetInt(pNode); //2
pNode = Json_locate(pJson:'/o/a[2]');
n = json_GetInt(pNode); //3
//To fetch the length of an array
pNode = Json_locate(pJson:'/o/a[UBOUND]');
n = json_GetInt(pNode); //5
n = json_GetInt(pJson:'/o/a[UBOUND]');
json_SetInt
Pointer json_SetInt( Pointer node : [String relativeNode] : [Int(20) newvalue] )
Parameters
- Pointer to existing node
- Relative path to node
- New value.
json_SetBool
Pointer json_SetInt( Pointer node : [String relativeNode] : [Ind newvalue] )
Parameters
- Pointer to existing node
- Relative path to node
- New value.
json_GetNext
Pointer json_GetNext( Pointer node )
Parameters
- Pointer to existing node
Returns next node in current block.
Example
{
o: {
e:[],
n:123,
a: [1,2,3,4,5],
m:"yyyy",
f: "john"
}
}
pJson = Json_ParseFile ('./test/documents/demo.json');
pArr = Json_locate(pJson: '/o/a[0]'); // First array element
dow (pArr <> *NULL) ;
n = Json_GetNum(pArr);
pArr = Json_GetNext(pArr);
endDo;
Json_Close(pJson);
json_GetName
Varchar(32767) json_GetName( Pointer node )
Parameters
- Pointer to existing node.
json_IsTrue
Ind json_IsTrue( Pointer node : String path )
Parameters
- Pointer to existing node
- Relative path to object or array
json_SetIterator
JSON_ITERATOR json_SetIterator( Pointer node : [String path] )
Parameters
- Pointer to existing node
- Relative path to object or array
Example
{
"a":123,
"b":"text",
"c":"More text"
}
pJson = Json_ParseFile('./test/documents/demo.json');
list = json_SetIterator(pJson);
dow json_ForEach(list);
n = json_GetName(list.this);
v = json_GetStr(list.this);
enddo;
json_ForEach
Ind json_ForEach( JSON_ITERATOR iterator )
Parameters
- Iterator struct from either
json_SetIterator
orjson_setRecursiveIterator
json_NodeDelete
Deletes the node and all of the node’s children.
void json_NodeDelete( Pointer node )
Note: This replaced json_Delete
, which is now deprecated.
Parameters
- Pointer to existing node.
json_Close
Close all nodes in this node.
void json_Close( Pointer node )
Parameters
- Pointer to existing node.