Serializing nested recursive JSON

Posted by dfranken on 11-Jul-2013 03:12

Hi guys,

I'm having trouble serializing nested JSON from a recursive data-relation, here is my example source code:

def temp-table ttFake

    field id as int

    field parentid as int

    field name as char.

def temp-table ttTest

    field id as int

    field parentid as int

    field name as char.

def dataset dsTest for ttTest

    data-relation drTest for ttTest, ttTest

    relation-fields (id, parentid) recursive.

create ttFake.

assign ttFake.id = 1

       ttFake.parentid = -1

       ttFake.name = "Root"

       .   

create ttFake.

assign ttFake.id = 2

       ttFake.parentid = 1

       ttFake.name = "Child1"

       .

create ttFake.

assign ttFake.id = 3

       ttFake.parentid = 1

       ttFake.name = "Child2"

       .

def query qFake for ttFake.

def data-source dsFake for query qFake ttFake keys(id).

buffer ttTest:attach-data-source(data-source dsFake:HANDLE).

query qFake:query-prepare("for each ttFake where ttFake.id = 1").

dataset dsTest:fill().

def var lcTest as longchar no-undo.

dataset dsTest:write-json("longchar", lcTest, true, "UTF-8", false, false).

disp lcTest view-as editor size-pixels 800 by 600 large

    with frame f1

    size-pixels 800 by 600.

Output:

{"dsTest": {

  "ttTest": [

    {

      "id": 1,

      "parentid": -1,

      "name": "Root"

    },

    {

      "id": 2,

      "parentid": 1,

      "name": "Child1"

    },

    {

      "id": 3,

      "parentid": 1,

      "name": "Child2"

    }

  ]

}}

So, the data-relation finds the children alright when I'm calling FILL().

The problem is that it flattens the result and doesn't nest them as would happen when using the NESTED keyword on the data-relation.

I can't use both NESTED and RECURSIVE on the same data-relation (no clue why), Progress gives me a syntax error when I try.

Is there any workaround for using both NESTED and RECURSIVE to create a nested JSON with recursive parent-child results?

All Replies

Posted by Robin Brown on 11-Jul-2013 15:00

Hi Dave,

As you've noted, we do not support serializing recursive data-relations with the WRITE-JSON or WRITE-XML methods.   As with navigation on a recursive dataset, you would have to use a recursive ABL procedure to navigate the records in the dataset and serialize the data yourself.  If you are on 11.0+, you could use the ABL JSON Api.

This question has come up before, but I don't know if there is an enhancement request for this.

Regards,

Robin

Posted by fromeverybody on 30-Apr-2019 13:41

Hi Robin,

What do you mean with using the ABL JSON Api? Should we still navigate the dataset recursively to create the json object? Or does this API have have something that handles the recusive nesting?

Posted by bronco on 30-Apr-2019 15:08

I think what Robin meant 6 years ago is JsonObject, JsonArray and those kind of objects in the Progress.Json.ObjectModel.* namespace.

This thread is closed