B
burnmw
Guest
I'm currently attempting to output a DataSet to a JsonObject, to match a given API. So, this is my example code: DEFINE VARIABLE lh_Relation AS HANDLE NO-UNDO. DEFINE VARIABLE lh_DataSet AS HANDLE NO-UNDO. DEFINE VARIABLE lobj_JsonObject AS JsonObject NO-UNDO. DEFINE VARIABLE llc_JsonOutput AS LONGCHAR NO-UNDO. lobj_JsonObject = NEW JsonObject(). DEFINE TEMP-TABLE tt_Object SERIALIZE-NAME "data" FIELD lc_Type AS CHARACTER SERIALIZE-NAME "type" FIELD lde_ID AS DECIMAL SERIALIZE-NAME "id" . DEFINE TEMP-TABLE tt_Attributes SERIALIZE-NAME "attributes" FIELD lde_ObjectID AS DECIMAL FIELD lc_Name AS CHARACTER SERIALIZE-NAME "name". CREATE DATASET lh_DataSet. lh_DataSet:NAME = "". lh_DataSet:SET-BUFFERS(BUFFER tt_Object:HANDLE, BUFFER tt_Attributes:HANDLE). lh_Relation = lh_DataSet:ADD-RELATION(BUFFER tt_Object:HANDLE, BUFFER tt_Attributes:HANDLE, "lde_ID,lde_ObjectID", FALSE, TRUE, TRUE, FALSE, TRUE). CREATE tt_Object. ASSIGN tt_Object.lc_Type = "driver" tt_Object.lde_ID = 1. CREATE tt_Attributes. ASSIGN tt_Attributes.lde_DriverID = 1 tt_Attributes.lc_Name = "Steve". lh_DataSet:WRITE-JSON ("FILE", "C:\temp\jsonOutput.txt", TRUE, "UTF-8", FALSE, TRUE). Which produces the following output: { "data": [ { "type": "driver", "id": 1.0, "attributes": [ { "name": "Steve" } ] } ] } I need to have it look like this: { "data": { "type": "driver", "id": 1.0, "attributes": { "name": "Steve" } } } The difference being that the temp-tables in the actual output become JsonArrays, before being added to the overall JsonObject - that isn't what I need/want. I can create the desired output by simply doing it manually - which for this example isn't too difficult, but the datasets that I will be working with are much more complex than this. Is it possible to do what I need to do, without resorting to overly complex manual processing? Thanks in advance!
Continue reading...
Continue reading...