jsonopenedgeprogress-4gl

How to properly build and append a json file from variables


I'm practising programming an application that takes user input and then outputs it to a json file.

I found a how to that explains how to do it. For the sake of length, I'm leaving out the input code and just including the json builder.

  ASSIGN 
            uComp = "testCompany"
            uEmail = "testEmail"
            uName = "testName"
            uAdd = "Additional"

        .


          DEFINE VARIABLE myObj         AS JsonObject NO-UNDO.
          DEFINE VARIABLE myData        AS JsonObject NO-UNDO.
          DEFINE VARIABLE dataParams      AS JsonObject NO-UNDO.
          DEFINE VARIABLE lResult       AS LONGCHAR NO-UNDO
              VIEW-AS EDITOR LARGE SIZE 60 BY 16.
          DEFINE VARIABLE lJArray       AS JsonArray  NO-UNDO.
          DEFINE VARIABLE lAnotherArray AS JsonArray  NO-UNDO.

          OUTPUT TO "output path.json".


          myObj = NEW JsonObject().
          dataParams = NEW JsonObject().



          myObj:Add("id", "01").

          dataParams:Add("Company_name", uComp).
          dataParams:Add("uEmail", uEmail).
          dataParams:add("uName", uName).
          dataParams:add("AddInfo", uAdd).


          lJArray = NEW JsonArray().
          lJArray:Add(dataParams).


          myObj:Add("data", lJArray).


          myObj:Write(lResult, TRUE).




          DISPLAY lResult.

That part works fine, but my output is like so:

lResult-----------------------------------------------------

{
  "id": "01",
  "data": [
    {
      "Company_name": "testCompany",
      "uEmail": "testEmail",
      "uName": "testName",
      "AddInfo": "Additional"
    }
  ]
}

how do I prevent the

lResult-----------

from being added to the file.

Secondly, I want to add additional information to the file when the code runs again so that the output will become.

{
  "id": "01",
  "data": [
    {
      "Company_name": "testCompany",
      "uEmail": "testEmail",
      "uName": "testName",
      "AddInfo": "Additional"
    },
     {
      "Company_name": "testCompany",
      "uEmail": "testEmail",
      "uName": "testName",
      "AddInfo": "Additional"
    }
  ]
}

What is the correct way to target a point in the file and add additional objects?

I though it might be something along the lines of an

append

property.


Solution

  • I would leave the complete JSON I/O to the JSON parser in the language. So instead of the append, I'd read in the file into a JSON object and add the additional objects/properties in memory and write back to a file.

    Just an output with append won't produce value JSON. This should work:

    FILE-INFORMATION:FILE-NAME = "myfile.json" .
    
    IF FILE-INFORMATION:FULL-PATHNAME > "":U THEN DO:
        myObj = CAST ((NEW ObjectModelParser()):ParseFile(FILE-INFORMATION:FULL-PATHNAME),
                      JsonObject) .
    
        lJArray = myObj:GetJsonArray("data") .
    END.
    ELSE DO:
        myObj = NEW JsonObject().
        myObj:Add("id", "01").
        lJArray = NEW JsonArray().
        myObj:Add("data", lJArray).
    END.
    
    dataParams = NEW JsonObject().
    dataParams:Add("Company_name", uComp).
    dataParams:Add("uEmail", uEmail).
    dataParams:add("uName", uName).
    dataParams:add("AddInfo", uAdd).
    
    lJArray:Add(dataParams).
    
    myObj:WriteFile("myfile.json", TRUE).