Skip to main content

TOJSON

TOJSON( record )

recordThe row (record) of data to convert to JSON format.
Return:TOJSON returns a STRING.

The TOJSON function returns a single string with the data in the record re-formatted as JSON. If the RECORD structure of the record has XPATHs defined, then they will be used, otherwise the lower-cased field names are used as the JSON tag names.

Example:

namesRec1 := RECORD  
 UNSIGNED2 EmployeeID{xpath('EmpID')};  
 STRING10 Firstname{xpath('FName')};  
 STRING10 Lastname{xpath('LName')}; 
END; 
str1 := TOJSON(ROW({42,'Fred','Flintstone'},namesRec1)); 
OUTPUT(str1); 
//returns this string: 
//'"EmpID": 42, "FName": "Fred", "LName": "Flintstone"' 
namesRec2 := RECORD  
  UNSIGNED2 EmployeeID;  
  STRING10 Firstname;  
  STRING10 Lastname; 
END; 
str2 := TOJSON(ROW({42,'Fred','Flintstone'},namesRec2)); 
OUTPUT(str2); 
//returns this string: 
//'"employeeid": 42, "firstname": "Fred", "lastname": "Flintstone"'

See Also: ROW, FROMJSON