Sun Oct 13, 2019 11:39 pm
Login Register Lost Password? Contact Us


toJson

Comments and questions related to the Enterprise Control Language

Tue Oct 08, 2019 1:34 pm Change Time Zone

I'm trying to use the tojson() command to form json in the following format. How can this be done?

Code: Select all
{
        "text": "Hello World",
   "services": [
      {
         "name": "ServiceA",
         "body": "default",
      },
      {
         "name": "ServiceB",
         "body": "custom"
      },
      {
         "name": "ServiceC",
         "body": "default"
      }
   ]
}


Code: Select all
RequestRec := record
   unicode text := (unicode)'';
   set of unicode services {xpath('services')}
end;
wjblack
 
Posts: 27
Joined: Mon Jul 11, 2016 12:45 pm

Tue Oct 08, 2019 2:02 pm Change Time Zone

wjblack,

Like this:
Code: Select all
ServiceRec := RECORD
  UNICODE name{xpath('name')};
  UNICODE body{xpath('body')};
END;
RequestRec := record
  UNICODE text{xpath('text')};
  DATASET(ServiceRec) services {xpath('services')}
end;

str1 := TOJSON(ROW({U'Hello World',
                   [{U'ServiceA',U'default'},
                    {U'ServiceB',U'custom'},
                    {U'ServiceC',U'default'}]},RequestRec));
OUTPUT(str1);

Repeating elements in both XML and JSON are expressed as nested child datasets in ECL.

HTH,

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

Tue Oct 08, 2019 2:18 pm Change Time Zone

Also: If you think of an ECL record as a JSON object, then you will need to wrap the result of TOJSON() in braces in order to get valid JSON:

Code: Select all
str1 := TOJSON(...);
str2 := U8'{' + str1 + U8'}';
OUTPUT(str2);

Also, note that TOJSON() returns UTF8, not STRING. That is why the "wrapping" is casting the brace characters to UTF8 in the above example.

Dan
DSC
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 565
Joined: Tue Oct 18, 2011 4:45 pm


Return to ECL

Who is online

Users browsing this forum: No registered users and 1 guest

cron