Manny
Manny

Reputation: 11

How to read a data value from a nested json "file" using Progress 4GL

I have the following JSON structure stored in a file, and I would like to read it dynamically and assign the values to a temp-table:

json

{
    "Reprint": {
        "RequestID": "00000001",
        "DeviceCode": "001",
        "Copies": "2"
    }
}

Problem: The direct use of READ-JSON is not working as expected, possibly because this JSON structure is nested, not a flat array.

Goal: I need to retrieve specific values, such as "DeviceCode", from the nested structure. How can I read this JSON dynamically and assign its values to my temp-table?

Upvotes: 0

Views: 181

Answers (2)

Jensd
Jensd

Reputation: 8021

I don't know if you simplified your file but that JSON is possible to read into a temp-table, using the serialize-name attribute of the temp-table for the

DEFINE TEMP-TABLE ttReprint SERIALIZE-NAME "reprint"
    FIELD requestID  AS CHARACTER
    FIELD DeviceCode AS CHARACTER 
    FIELD Copies     AS CHARACTER.

TEMP-TABLE ttReprint:READ-JSON("file", "file.json").

FOR EACH ttReprint:
    DISPLAY ttReprint.
END.

https://abldojo.services.progress.com/?shareId=66fd33d3f3309a3dc1fbb14b

Upvotes: 1

Stefan Drissen
Stefan Drissen

Reputation: 3379

Assuming you are not on an ancient version of Progress OpenEdge you can use the Progress.Json.ObjectModel classes to parse your file.

The ObjectModelParser imports the file and casts it to a JsonObject, the inner 'Reprint' object is then extracted and its properties are then printed:

using Progress.Json.ObjectModel.*.

def var oparser as ObjectModelParser no-undo.
def var ojson   as JsonObject no-undo.

oparser = new ObjectModelParser().

ojson = cast( oparser:ParseFile( 'file.json' ), JsonObject ).

ojson = ojson:getJsonObject( 'Reprint' ).

message
    ojson:getCharacter( 'RequestID' )
    ojson:getCharacter( 'DeviceCode' )
    ojson:getCharacter( 'Copies' )
    .

Watch it fly at https://abldojo.services.progress.com/?shareId=66fc1475f3309a3dc1fbb038

Upvotes: 3

Related Questions