Salesforce

How to import a JSON object using ParseFile?

« Go Back

Information

 
TitleHow to import a JSON object using ParseFile?
URL Name000040659
Article Number000168625
EnvironmentProduct: OpenEdge
Version: 11.x, 12.x
OS: All Supported Platforms
Question/Problem Description

How to import a JSON object using ParseFile?
 
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution

The below example code demonstrates how to import the .json file attached to this article.  This uses the ParseFile() method, then iterates through the object, reading and creating records in a TEMP-TABLE.  Note that this method implies previous knowledge of the structure of the file.
USING Progress.Json.ObjectModel.ObjectModelParser.
USING Progress.Json.ObjectModel.JsonArray.
USING Progress.Json.ObjectModel.JsonObject.

DEFINE VARIABLE oParser     AS ObjectModelParser NO-UNDO.
DEFINE VARIABLE oJsonArray  AS JsonArray         NO-UNDO.
DEFINE VARIABLE oJsonObject AS JsonObject        NO-UNDO.

DEFINE VARIABLE iCount      AS INTEGER           NO-UNDO.
DEFINE VARIABLE iLength     AS INTEGER           NO-UNDO.

DEFINE VARIABLE lcSection   AS LONGCHAR          NO-UNDO.
DEFINE VARIABLE cFile       AS CHARACTER         NO-UNDO EXTENT 3.
DEFINE VARIABLE lcJson      AS LONGCHAR           NO-UNDO.

DEFINE TEMP-TABLE ttJsonRecord NO-UNDO
    FIELD jsonFileName        AS CHARACTER
    FIELD nameProperty        AS CHARACTER
    FIELD statusProperty      AS CHARACTER
    FIELD ageProperty         AS INTEGER
    FIELD codeProperty        AS CHARACTER
    FIELD codeSystemProperty  AS CHARACTER
    FIELD codeSysNameProperty AS CHARACTER.


COPY-LOB FILE "input.json" TO lcJson CONVERT TARGET CODEPAGE "UTF-8".
oParser = NEW ObjectModelParser().
oJsonArray = CAST(oParser:Parse(lcJson), JsonArray).  

iLength = oJsonArray:Length.
DO iCount = 1 TO iLength:
  oJsonObject = oJsonArray:GetJsonObject(iCount).
  CREATE ttJsonRecord.
  ASSIGN ttJsonRecord.jsonFileName        = cFile[1]
         ttJsonRecord.nameProperty        = oJsonObject:GetCharacter("name")
         ttJsonRecord.statusProperty      = oJsonObject:GetCharacter("status")
         ttJsonRecord.ageProperty         = oJsonObject:GetInteger("age")
         ttJsonRecord.codeProperty        = oJsonObject:GetCharacter("code")
         ttJsonRecord.codeSystemProperty  = oJsonObject:GetCharacter("code_system")
         ttJsonRecord.codeSysNameProperty = oJsonObject:GetCharacter("code_system_name").
END.

DELETE OBJECT oJsonArray NO-ERROR.
DELETE OBJECT oParser    NO-ERROR.
DELETE OBJECT oJsonObject  NO-ERROR.

FOR EACH ttJsonRecord:
  DISPLAY ttJsonRecord WITH 1 COLUMN SIDE-LABELS.
END.
Workaround
Notes
Keyword Phrase
Last Modified Date8/21/2024 7:38 AM

Powered by