Searching for objects in JSON

Posted by Neil Treeby on 13-Mar-2019 18:00

I have some sample C# code that uses syntax like this to get values out of a JSON entity.

If I have JSON like this:

{ "Object1": { "Array2" [ {"Ref": "someRef", "Value": "someValue"}, {"Ref": "someOtherRef", "Value": "someOtherValue"} ] } }

The C# code looks like:

var someValue = root.Object1.Array2.First(stuff => stuff.Ref = "someRef").Value;

What that gets me is: someValue = "someValue"

So far the equivalent in ABL code would be:

ostuff = ojson:GetJsonObject("Object1"):GetJsonArray("Array2").

DO viLinkIdx = 1 TO ostuff:LENGTH:

    CASE ostuff:GetJsonObject(viLinkIdx):GetJsonText("Ref"):

        WHEN "someRef" THEN ASSIGN vcSomeValue = ostuff:GetJsonObject(viLinkIdx):GetJsonText("Value").


I wind up with the same result: vcSomeValue = "someValue"

Now, am I missing something, or is there an easier way to do this kind of searching in JSON using ABL?  Before I start to extend the JsonObject class to do something similar?

All Replies

Posted by Mike Fechner on 13-Mar-2019 18:02

You cannot extend the JsonObject directly.
You’ll have to build your own helper class.

Posted by Neil Treeby on 13-Mar-2019 19:48

I was able to successfully extend both JsonArray and JsonObject (and have r-code to prove it), but the reason a helper class is needed is because I can't cast up from JsonConstruct, JsonArray or JsonObject to my new class in order to access my methods.

Posted by bronco on 14-Mar-2019 10:15

The tendency is to move away from inheritance anyway for all sorts of reasons, so a helper class is a wise decision.

Posted by Patrick Tingen on 14-Mar-2019 10:25

The problem is not so much with the JSON class being in need of upgrade, but more the whole support for arrays in the ABL. If you  look in languages like JS, it strikes me that they have such fancy methods to search and filter their arrays.

This thread is closed