Reputation: 3593
I am trying to parse a nested json object as string with the class /ui2/cl_json in ABAP... However, it seems it does not get all the data parsed from the json:
EDIT: I found the solution, I just neede a single line to define the type which is exactly my table type:
DATA: zalm_local_user TYPE STANDARD TABLE OF zxyz_fs_users.
I then can safely pass that type to my json deserialize function and it simply works also with nested fields.... IMPORTANT: you have to give your .include a group name, in my case "address" to recognize it as a named "address" substructure...
{
"id":1,
"uuid":"3423e00f-b5c2-4f2c-bf88-baceca11c5f4",
"firstName":"Isabel",
"lastName":"Zapletal",
"fullName":"Eddi Engel",
"gender":"männlich",
"username":"Annabelle12",
"email":"[email protected]",
"avatar":"https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/221.jpg",
"password":"4GYJ5LYoXrEmpZU",
"birthdate":"30.4.1988",
"registeredAt":"2023-02-26T10:49:56.165Z",
"phone":"97-1365-5739",
"jobTitle":"Corporate Brand Executive",
"jobType":"Agent",
"profileInfo":"Hi, my name is Bo Neuendorf.\n I was born in Tue May 14 1968 03:04:13 GMT+0100 (Mitteleuropäische Sommerzeit) and I am currently working as a Chief Branding Officer at Rossberg Gruppe.\n Check out my site on exzessiv-erwachsener.ch and contact me any time at +79 799 514 8382. Vitae unde tempora dolore a magnam. Consequatur deleniti veniam unde porro voluptates harum exercitationem cum reprehenderit. Eos ullam dignissimos laborum veniam voluptas consequuntur. Deleniti tempora sed veritatis ipsam laborum blanditiis. Vero vitae distinctio aut ea nihil. Soluta unde inventore.",
"address":{
"country":"Kroatien",
"county":"Buckinghamshire",
"city":"Neu Giuliadorf",
"streetAddress":"Am Quettinger Feld 977 Zimmer 152",
"latitude":7.6716,
"longitude":-70.7275,
"coordinates":[
83.8491,
-96.1326
],
"longLat":[
1.09844,
41.6314
],
"timezone":"America/Sao_Paulo",
"zipCode":"99171"
},
"maybe":"Yeah I'm here!",
"products":[
85,
8,
15
],
"companyId":20
}
EDIT: I replaced the previous image. The fields for address object are filled now. However I don't know how to insert that structure (with sub-address object) into my dictionary table of users (because tables are allowed to have only flat fields as it seems..) I included my "address" structure as ".INCLUDE" field into my table, but it's not recognized as the "address" field object, so I probably would have to rewrite my structures to fit my table structure and insert line by line..
here is my abap report code:
TYPES: begin of address,
country TYPE char30,
county type char30,
city type char50,
streetAddress type string,
latitude type dec015,
longitude type dec015,
* coordinates TYPE ty_coord,
* longLat TYPE ty_coord,
* timezone type char50,
zipcode type int4,
END of address.
TYPES: BEGIN OF ls_user,
mandt TYPE mandt,
id TYPE num05_kk2,
uuid TYPE SYSUUID_C36,
firstName TYPE char30,
lastName type char30,
fullName type char50,
gender type char10,
username type char30,
email type AD_SMTPADR,
avatar type string,
password type char50,
birthdate type char10,
registeredAt type char30,
phone type char30,
jobTitle type char50,
jobType type char30,
profileInfo type string,
address type address,
* country TYPE zalm_fs_address-country,
* county type zalm_fs_address-county,
* city type zalm_fs_address-city,
* streetAddress type zalm_fs_address-streetaddress,
* latitude type zalm_fs_address-latitude,
* longitude type zalm_fs_address-longitude,
** coordinates TYPE ty_coord,
** longLat TYPE ty_coord,
** timezone type char50,
* zipcode type zalm_fs_address-zipcode,
maybe type char30,
* products Type STANDARD TABLE OF int4 with non-UNIQUE default key,
* companyId: type char5,
END OF ls_user.
DATA lv_dauer TYPE i.
DATA lt_user TYPE STANDARD TABLE OF ls_user WITH NON-UNIQUE DEFAULT KEY.
DATA wa_user TYPE ls_user.
*DATA: lv_json TYPE string.
DATA(lv_json) =
`[{"id":1,"uuid":"3423e00f-b5c2-4f2c-bf88-baceca11c5f4","firstName":... }]`
* JSON -> ABAP (iTab)
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_json
* jsonx =
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
* assoc_arrays =
* assoc_arrays_opt =
* name_mappings =
* conversion_exits =
* hex_as_base64 =
CHANGING
data = lt_user
).
**cl_demo_output=>write_data( lv_json ).
*cl_demo_output=>write_data( lt_user[ 4 ]-address ).
**
*cl_demo_output=>write_data( lt_user[ 5 ]-jobtitle ).
*cl_demo_output=>display( lt_user[ 6 ]-profileinfo ).
DELETE FROM zalm_fs_users.
get run time field lv_dauer.
INSERT zalm_fs_users FROM TABLE lt_user. "ACCEPTING DUPLICATE KEYS.
get run time field lv_dauer.
Write lv_dauer.
I dont know if there are any better deserialize / parse options in the /ui2/cl_json class or how to do it otherwise... any ideas?
Upvotes: 0
Views: 2160
Reputation: 3593
See my edits above...
DATA: zalm_local_user TYPE STANDARD TABLE OF zxyz_fs_users.
was enough to declare my table type and to pass it down to my json deserialize function as import... and thats it! And don't forget.... you have to give your .include a group name, in my case "address" to recognize it as a named "address" substructure...
Upvotes: 1