Mahesh
Mahesh

Reputation: 127

Create JSON with dynamic key (Progress 4GL)

I have a dataset defined for 2 temp-tables linked with some field (lets say : item). For example:

define temp-table items no-undo
  field item as char.

define temp-table customer no-undo
  field item     as char serialize-hidden
  field custname as char
  field price    as dec.

define dataset dsitemcust for items,customer
  data-relation dr1 for items,customer relation-fields(item,item) nested.

This gives the json output like this:

{
  "items": [
     {
        "item": "abc_item",
        "customer": [
           {
              "custname": "uvw_cust",
              "price": 123
           },
           {
              "custname": "xyz_cust",
              "price": 234
           }, 
           ....
        ]
     },
     {
        "item": "def_item",
        "custname": [{},{},...]
     }
     ...
     ]
}

But i want to get something like this: [item as key and, custname and price as value (in array of object where custnum is key again)]

{ "abc_item" : [{"uvw_cust" : 123}, {"xyz_cust" : 234}, ...],
  "def_item" : [{}, .. ],
  ..
}

Is this possible/achievable in Progress openedge? (Progress version: 10.2B)

Upvotes: 0

Views: 3786

Answers (1)

TheDrooper
TheDrooper

Reputation: 1217

It can be done, but you'll have to manually build the JSON object.

USING Progress.Json.ObjectModel.*.

DEFINE VARIABLE oJson AS JsonObject NO-UNDO.
DEFINE VARIABLE oArray AS JsonArray NO-UNDO.
DEFINE VARIABLE oRec AS JsonObject NO-UNDO.

DEFINE TEMP-TABLE items NO-UNDO
  FIELD item AS CHARACTER.

DEFINE TEMP-TABLE customer NO-UNDO 
  FIELD item     AS CHARACTER
  FIELD custname AS CHARACTER
  FIELD price    AS DECIMAL.

/* Create some records here. */

oJson = NEW JsonObject().

FOR EACH items NO-LOCK:

    oArray = NEW JsonArray().

    FOR EACH customer WHERE customer.item = items.item NO-LOCK BREAK BY customer.item:
        oRec = NEW JsonObject().
        oRec:ADD(customer.custname, customer.price).
        oArray:ADD(oRec).
    END.

    oJson:ADD(items.item, oArray).
END.

oJson:WriteFile("test.json", TRUE).

This writes the oJson object to a file, but you can use it as your output.

Upvotes: 3

Related Questions