Wed Feb 20, 2019 1:57 am
Login Register Lost Password? Contact Us


Native TOJSON usage

Comments and questions related to the Enterprise Control Language

Fri Jan 25, 2019 6:15 pm Change Time Zone

I'm trying to determine how to format the below json using the TOJSON() call. Text and languageCode can easily be represented but how does one represent multiple vales for a key like with friuts?

Code: Select all
{
   "text": "My Basket",
   "languageCode": "en",
   "fruits": [
        "APPLES",
        "ORANGES",
        "BANANAS",
            ]
}


For example,
Code: Select all
namesRec1 := RECORD 
UNSIGNED2 EmployeeID{xpath('EmpID')}; 
STRING10  Firstname{xpath('FName')}; 
STRING10  Lastname{xpath('LName')};
END;
str1 := TOJSON(ROW({42,'Fred','Flintstone'},namesRec1));
OUTPUT(str1);

becomes
Code: Select all
"EmpID": 42, "FName": "Fred", "LName": "Flintstone"


Essentially how do you represent a json array
Code: Select all
{
"employees":[ "John", "Anna", "Peter" ]
}
wjblack
 
Posts: 12
Joined: Mon Jul 11, 2016 12:45 pm

Sun Jan 27, 2019 12:57 am Change Time Zone

While it's not the ideal way to represent a json mapping I was able to find a workaround by doing the following. It would be ideal if there was a way just using the TOJSON method.

Code: Select all
rec := RECORD 
STRING text;
STRING2 languageCode;
STRING fruits
END;

ds := dataset([
   {'My Basket', 'en', 'APPLES'},
    {'My Basket', 'en', 'ORANGES'},
    {'My Basket', 'en', 'BANANAS'}
],rec);

ds;

rec2 := {
STRING text;
STRING2 languageCode;
set of string fruits {xpath('fruits')}
};

ds2 := project(ds,
               transform(rec2,
                         self.fruits := [left.fruits],
                         self := left));

ds2;

ds3 := rollup(ds2,
              left.text = right.text and
                     left.languageCode = right.languageCode,
              transform(rec2,
                        self.fruits := left.fruits + right.fruits,
                        self := left));
                                    
ds3;

rec3 := {
  unicode jsonRow
};         
         
ds4 := project(ds3,
               transform(rec3,
                         self.jsonRow := '{' + toJson(left) + '}'));
                                    
ds4;


The result is
Code: Select all
{"text": "My Basket", "languagecode": "en", "fruits": ["APPLES", "ORANGES", "BANANAS"]}

wjblack
 
Posts: 12
Joined: Mon Jul 11, 2016 12:45 pm

Mon Jan 28, 2019 4:32 pm Change Time Zone

wjblack,

Your problem was with your initial RECORD Structure and inline DATASET. If you need a SET in the JSON, then it makes sense to start with a SET in ECL. Here's a way you can do what you want:
Code: Select all
rec := RECORD 
  STRING        text;
  STRING2       languageCode;
  SET OF STRING fruits
END;
ds := dataset([{'My Basket', 'en', ['APPLES','ORANGES','BANANAS']}],rec);
ds;
str1 := TOJSON(ds[1]);
OUTPUT(str1);
//Produces:
//{"text": "My Basket", "languagecode": "en", "fruits": {"Item": ["APPLES", "ORANGES", "BANANAS"]}

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1409
Joined: Wed Oct 26, 2011 7:40 pm


Return to ECL

Who is online

Users browsing this forum: No registered users and 1 guest

cron