Sun Apr 21, 2019 8:08 am
Login Register Lost Password? Contact Us


FROMJSON & json array

Comments and questions related to the Enterprise Control Language

Fri Feb 08, 2019 7:35 pm Change Time Zone

How can I use the fromjson() command to read in a json array into ecl.

Code: Select all
{
   "users": [
      {
         "username": "SammyShark",
         "location": "Indian Ocean"
      },
      {
         "username": "JesseOctopus",
         "location": "Pacific Ocean"
      },
      {
         "username": "DrewSquid",
         "location": "Atlantic Ocean"
      },
      {
         "username": "JamieMantisShrimp",
         "location": "Pacific Ocean"
      }
   ]
}
wjblack
 
Posts: 21
Joined: Mon Jul 11, 2016 12:45 pm

Fri Feb 08, 2019 8:05 pm Change Time Zone

There is nothing else in the primary record, and there is only one, but how about this to treat "users" as a child dataset?

Code: Select all
jsonstring   :=
  '{'
+ '   "users": ['
+ '      {'
+ '         "username": "SammyShark",'
+ '         "location": "Indian Ocean"'
+ '      },'
+ '      {'
+ '         "username": "JesseOctopus",'
+ '         "location": "Pacific Ocean"'
+ '      },'
+ '      {'
+ '         "username": "DrewSquid",'
+ '         "location": "Atlantic Ocean"'
+ '      },'
+ '      {'
+ '         "username": "JamieMantisShrimp",'
+ '         "location": "Pacific Ocean"'
+ '      }'
+ '   ]'
+ '}';

rUsers   :=
record
  string  username{xpath('username')};
  string  location{xpath('location')};
end;
rMain :=
record
  dataset(rUsers)  Users{xpath('users')};
end;
fromjson(rMain, jsonstring);
Tony Kirk
 
Posts: 14
Joined: Thu Jun 23, 2011 5:01 pm

Fri Feb 08, 2019 8:16 pm Change Time Zone

Or, to your original question that actually has a field in the primary record (still only one record, though), and after adding some missing commas:

Code: Select all
jsonstring   :=
  '{'
+ '   "gender": ['
+ '      {'
+ '         "type": "male",'
+ '         "age": 25,'
+ '         "name": "will"'
+ '      },'
+ '      {'
+ '         "type": "female",'
+ '         "age": 21,'
+ '         "name": "wilma"'
+ '      }'
   + '],'
+ '   "requestId": "8675309"'
+ '}';

rGender   :=
record
  string    type{xpath('type')};
  unsigned  age{xpath('age')};
  string    name{xpath('name')};
end;

rMain   :=
record
  dataset(rGender)  Gender{xpath('gender')};
  string            ReqeuestId{xpath('requestId')};
end;

fromjson(rMain, jsonstring);
Tony Kirk
 
Posts: 14
Joined: Thu Jun 23, 2011 5:01 pm

Sat Feb 09, 2019 4:33 pm Change Time Zone

Thanks... The premise of the root question is to pull in a json file of this format. I'm hoping the recommendation will work for this as well. There are different tags under each entity that's returned back.

Code: Select all
{
   "entities": [
      {
         "attributes": {
            "firstName": "John",
            "gender": "M",
            "lastName": "Smith"
         },
         "count": 1,
         "label": "John Smith",
         "mentions": [
            {
               "extractedText": "John Smith",
               "position": [
                  0,
                  11
               ]
            }
         ],
         "type": "PERSON"
      },
      {
         "some_other_attributes": {
            "id": "12345",
            "population": 200000
         },
         "count": 1,
         "label": "Iowa",
         "mentions": [
            {
               "extractedText": "Iowa",
               "position": [
                  21,
                  25
               ]
            }
         ],
         "type": "GEO"
      }
   ],
   "requestId": "8675309"
}
wjblack
 
Posts: 21
Joined: Mon Jul 11, 2016 12:45 pm


Return to ECL

Who is online

Users browsing this forum: No registered users and 0 guests

cron