{"Customer":{"Id":"45f2d341-5bec-e611-811f-70106fa9e1e1","StudentInsurance":false,"WantsPackage":true,"InsuranceUnder28":false,"CadetInsurance":false,"WantsTravelInsurance":true,"InsuranceHealth":false,"IsCollectiveSuperContentInsurance":true}}
def var dsh as handle no-undo.
create dataset dsh.
dsh:READ-JSON ('longchar',theFileContent).
trying to load it into a dataset usring dsh:read-json('longchar',myContent).
Getting errror 15358 - Error parsing JSON: expected brace, but found string.
The correct format for JSON for a dataset would be:
{"dataset":
{
"temptable1":
[{"field1": "vaue1", ...},
{"field1": "value1", ...}],
"temptable2":
[{"field2": "value2", ...},
{"field2": "value2", ...}]
}
}
Each temp-table in the dataset is a separate object.
The temp-table data is represented by a JSON array, with each row of a the temp-table represented by an object in the array.
It is probably because each jsonobject within is a one record object and not presented as an array. If so, is it only me that find it a bit weak that it can't load this type of jsonobjects?
Do I have to make my own loader?
If you know that that you have an object with a record, then you can add the record to an array and use the expected structure for a temp-table or dataset.
Example:
USING PROGRESS.Json.ObjectModel.*. DEFINE VARIABLE cData AS CHARACTER NO-UNDO. DEFINE VARIABLE jsonParser AS ObjectModelParser NO-UNDO. DEFINE VARIABLE jsonObject AS JsonObject NO-UNDO. DEFINE VARIABLE jsonArray AS JsonArray NO-UNDO. DEFINE TEMP-TABLE tt NO-UNDO FIELD Id AS CHARACTER FORMAT "x(40)" FIELD StudentInsurance AS LOGICAL FIELD WantsPackage AS LOGICAL FIELD InsuranceUnder28 AS LOGICAL FIELD CadetInsurance AS LOGICAL FIELD WantsTravelInsurance AS LOGICAL FIELD InsuranceHealth AS LOGICAL // FIELD IsCollectiveSuperContentInsurance AS LOGICAL . DEFINE DATASET ds FOR tt. cData = '~{"Customer":~{"Id":"45f2d341-5bec-e611-811f-70106fa9e1e1","StudentInsurance":false,"WantsPackage":true,"InsuranceUnder28":false,"CadetInsurance":false,"WantsTravelInsurance":true,"InsuranceHealth":false,"IsCollectiveSuperContentInsurance":true~}~}'. jsonParser = NEW ObjectModelParser(). jsonObject = CAST(JsonParser:Parse(cData), JsonObject). jsonArray = NEW JsonArray(). jsonArray:ADD(jsonObject:GetJsonObject("Customer")). jsonObject = NEW JsonObject(). jsonObject:ADD("tt", jsonArray). TEMP-TABLE tt:READ-JSON("JsonObject", jsonObject). // DATASET ds:READ-JSON("JsonObject", jsonObject). FOR EACH tt: DISPLAY tt. END.
Regarding Edsel's example above. READ-JSON of the character data directly will also work, if you change cData to a longclar and add SERIALIZE-NAME to the temp-table definition.
define variable cData AS longchar no-undo.
...
define temp-table tt no-undo serialize-name "Customer"
...
temp-table tt:read-json("longchar", cData).
...