so, here is my little method trying to parse a json object passed in as a parameter to the method.
I have
def var lv_Property as char extent no-undo.
lv_Property = oOptions:GetNames(). /** oOptions is of type jsonObject */
do lv_i = 1 to extent(lv_Property):
/** question: how do I know the datatype of property[lv_i]. as I need to do either
GetCharacter(), getDate() etc , but the properties are of a mixed datatype (some are boolean for example */
end.
so, how to I do this where I do not know what the names of the properties are, nor do I know in advance what datatype each property is.
11.3.1
thanks!
Hi Julian,
Connect to the sports database and run the code I've added bellow.
For getting the json child element which could be of the type JsonObject
or perhaps JsonArray, you would catch the error using Progress.Json.JsonError that would
be returned and then process other types. Hope this answers at least some of your questions.
USING Progress.Json.ObjectModel.* .
DEFINE VARIABLE myObj AS JsonObject NO-UNDO.
DEFINE VARIABLE myElem AS JsonObject NO-UNDO.
DEFINE VARIABLE myArray AS JsonArray NO-UNDO.
DEFINE VARIABLE myLongchar AS LONGCHAR NO-UNDO.
DEFINE VARIABLE lv_Property AS CHARACTER EXTENT no-undo.
DEFINE VARIABLE lv_Property1 AS CHARACTER EXTENT no-undo.
DEFINE VARIABLE lv_i AS INTEGER NO-UNDO.
DEFINE VARIABLE lv_i2 AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE ttCust
FIELD CustNum AS INTEGER
FIELD Name AS CHARACTER
FIELD CreditLimit AS DECIMAL
FIELD someLogic AS LOGICAL .
/* . . Load temp-table . */
FOR EACH Customer WHERE CustNum < 5 NO-LOCK:
CREATE ttCust.
BUFFER-COPY Customer to ttCust.
END.
/* Let's create json object.. */
myObj = NEW JsonObject().
/* ..and load it with ttCust data */
myObj:Read(TEMP-TABLE ttCust:HANDLE).
/* we can get names of all properties within json object */
lv_Property = myObj:GetNames().
/* Here you get message "ttCust" as this json object contains one element */
/* and that is JsonArray with its elements being ttCust records */
MESSAGE "Number of elements in root json object is " EXTENT(lv_Property)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* Let's write our json to a file so that we can examine it's structure */
myObj:WRITEfile("ttCust.txt") .
/* so let's fetch our array from myObj's array property) */
myArray = myObj:GetJsonArray("ttCust") .
/* and let's report on it's length */
MESSAGE "Array's length is " myArray:Length
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* Let's go through the json array elements */
DO lv_i = 1 TO myArray:Length :
myElem = myArray:GetJsonObject(lv_i) .
lv_Property1 = myElem:GetNames().
DO lv_i2 = 1 TO EXTENT(lv_Property1) :
MESSAGE "Property " lv_Property1[lv_i2] " has type : " myElem:GetType(lv_Property1[lv_i2])
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
END.
/* For other data types please check */
/* Progress.Json.ObjectModel.JsonDataType class */
/* Array property */
/* String property */
/* Number property */
/* Boolean property */
/* Object property */
/* Null property */
Hi Julian,
Connect to the sports database and run the code I've added bellow.
For getting the json child element which could be of the type JsonObject
or perhaps JsonArray, you would catch the error using Progress.Json.JsonError that would
be returned and then process other types. Hope this answers at least some of your questions.
USING Progress.Json.ObjectModel.* .
DEFINE VARIABLE myObj AS JsonObject NO-UNDO.
DEFINE VARIABLE myElem AS JsonObject NO-UNDO.
DEFINE VARIABLE myArray AS JsonArray NO-UNDO.
DEFINE VARIABLE myLongchar AS LONGCHAR NO-UNDO.
DEFINE VARIABLE lv_Property AS CHARACTER EXTENT no-undo.
DEFINE VARIABLE lv_Property1 AS CHARACTER EXTENT no-undo.
DEFINE VARIABLE lv_i AS INTEGER NO-UNDO.
DEFINE VARIABLE lv_i2 AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE ttCust
FIELD CustNum AS INTEGER
FIELD Name AS CHARACTER
FIELD CreditLimit AS DECIMAL
FIELD someLogic AS LOGICAL .
/* . . Load temp-table . */
FOR EACH Customer WHERE CustNum < 5 NO-LOCK:
CREATE ttCust.
BUFFER-COPY Customer to ttCust.
END.
/* Let's create json object.. */
myObj = NEW JsonObject().
/* ..and load it with ttCust data */
myObj:Read(TEMP-TABLE ttCust:HANDLE).
/* we can get names of all properties within json object */
lv_Property = myObj:GetNames().
/* Here you get message "ttCust" as this json object contains one element */
/* and that is JsonArray with its elements being ttCust records */
MESSAGE "Number of elements in root json object is " EXTENT(lv_Property)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* Let's write our json to a file so that we can examine it's structure */
myObj:WRITEfile("ttCust.txt") .
/* so let's fetch our array from myObj's array property) */
myArray = myObj:GetJsonArray("ttCust") .
/* and let's report on it's length */
MESSAGE "Array's length is " myArray:Length
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* Let's go through the json array elements */
DO lv_i = 1 TO myArray:Length :
myElem = myArray:GetJsonObject(lv_i) .
lv_Property1 = myElem:GetNames().
DO lv_i2 = 1 TO EXTENT(lv_Property1) :
MESSAGE "Property " lv_Property1[lv_i2] " has type : " myElem:GetType(lv_Property1[lv_i2])
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
END.
/* For other data types please check */
/* Progress.Json.ObjectModel.JsonDataType class */
/* Array property */
/* String property */
/* Number property */
/* Boolean property */
/* Object property */
/* Null property */