FROMJSON( record, jsonstring ,[ONFAIL(transform )])
record | The RECORD structure to produce. Each field should specify the XPATH to the data in the jsonstring that it should hold. If omitted, the lower-cased field names are used. |
jsonstring | A string containing the JSON to convert. |
ONFAIL | Optional. Specifies a transform to handle errors in the JSON. |
transform | A TRANSFORM structure matching the record structure of the first parameter. |
Return: | FROMJSON returns a single row (record). |
The FROMJSON function returns a single row (record) in the record format from the specified jsonstring. This may be used anywhere a single row can be used (similar to the ROW function).
Example:
namesRec := RECORD
UNSIGNED2 EmployeeID{xpath('EmpID')};
STRING10 Firstname{xpath('FName')};
STRING10 Lastname{xpath('LName')};
END;
x := '{"FName": "George" , "LName": "Jetson", "EmpID": 42}';
rec := FROMJSON(namesRec,x);
OUTPUT(rec);
Example with Error handling and bad JSON:
namesRec := RECORD
UNSIGNED2 EmployeeID{xpath('EmpID')};
STRING20 Firstname{xpath('FName')};
STRING20 Lastname{xpath('LName')};
END;
x := '{"FName": "malformedJSON""George" , "LName": "Jetson", "EmpID": 42}';
namesRec createFailure() :=
TRANSFORM
SELF.FirstName := FAILMESSAGE;
SELF := [];
END;
rec := FROMJSON(namesRec,x,ONFAIL(createFailure()));
OUTPUT(rec);