Answered check for existance of Json value before reading to temp-table

Potish

Member
I am reading data from a json file into a temp-table provided by an external vendor. Some fields on the json file are not included every time. How do I check if a field exists on the json file before I attempt to read it. A simplified version of my code looks as follows

Code:
USING Progress.Json.ObjectModel.ObjectModelParser.
USING Progress.Json.ObjectModel.JsonArray.
USING Progress.Json.ObjectModel.JsonObject.

DEFINE VARIABLE oParser             AS ObjectModelParser NO-UNDO.
DEFINE VARIABLE oavailabilityArray  AS JsonArray         NO-UNDO.
DEFINE VARIABLE oavailabilityObject AS JsonObject        NO-UNDO.

define temp-table ttHAPIavailability like HAPIavailability.

oParser  = NEW ObjectModelParser().
oavailabilityObject = CAST(oParser:ParseFile("testfile.json"), JsonObject).

create ttHAPIavailability.
assign
    ttHAPIavailability.price = decimal(oavailabilityObject:GetCharacter("price"))
    ttHAPIavailability.currency = oavailabilityObject:GetCharacter("currency")
    ttHAPIavailability.additional_info = oavailabilityObject:GetCharacter("additional_info").

In this case 'additional_info' is not always passed so if I don't check prior to reading it I end up with the following error

Call to Progress.Json.ObjectModel.JsonObject:GetCharacter( ) failed. Property 'additional_info' was not found. (16058)
 

TheMadDBA

Active Member
Add NO-ERROR to your assign or CATCH the errors.

Why not use READ-JSON on the temp-table instead? It handles all of that for you.
 

mollyfud

Member
I haven't tried it myself, but there is a "HAS" method that is described as:
Returns a LOGICAL indicating if the JsonObject contains the named property.

Takes a character input of the property name. Wonder if you could check if the property exists and if it does, setting it.

Hope this helps.
TIA
Molly
 

Potish

Member
Quick update. Adding the NO-ERROR worked for now.

The reason I asked about this option is because the vendor I am working sometimes nests the json in a way that READ-JSON is not able to support so I wanted to have the information for those cases. Where I can I always use the READ-JSON.

I will look into the HAS option and update this thread if it works.

Thank you for the suggestions.
 
Top