02-04-2021 8:37 AM
Hi Expert,
Please let me know if it possible split string to itab without removing character in string ?
i have variable like this,
DATA : response TYPE string VALUE '{"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489"},{"success":"true","msg":" valid","matnr":"PH X 15","matkl":"123433"}',
and then i do split,
SPLIT response AT '},' INTO TABLE DATA(lv_line).
LOOP AT lv_line INTO ls_line.
CALL METHOD cl_fdt_json=>json_to_data
EXPORTING
iv_json = ls_line
CHANGING
ca_data = ls_data_json.
APPEND ls_data_json TO lt_data_json.
ENDLOOP.
when i using cl_fdt_json , field matkl at first row is initial.
because the value string as be : {"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489".
but i need value as be : {"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489"} <--
02-04-2021 2:23 PM
TYPES : BEGIN OF ty_data,
success TYPE string,
msg TYPE string,
matnr TYPE matnr,
matkl TYPE matkl,
END OF ty_data.
DATA : ls_data TYPE ty_data.
lt_data TYPE STANDARD TABLE OF ty_data.
DATA : lv_response TYPE string VALUE '[{"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489"},{"success":"true","msg":" valid","matnr":"PH X 15","matkl":"123433"}]',
/ui2/cl_json=>deserialize( EXPORTING json = lv_response
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING data = lt_data ).
your string data is is json data so you can use json deserialize method.
After the deserialize your internal table wil be like this.
If you need json data you can use serialize method
DELETE lt_data2 INDEX 1.
/ui2/cl_json=>serialize( EXPORTING data = lt_data RECEIVING r_json = DATA(r_json) ).
02-04-2021 8:47 AM
Hello jandrivay
The separator is ignored in the results. Since you're using the } as a part of the separator it is removed. You could append it to the end of the line before parsing the JSON.
Kind regards,02-04-2021 9:12 AM
You could just replace the "},{"-part(s) of the [response]-string by "}#{", and then SPLIT .. AT '#'..
That way the only character that gets removed by the SPLIT keyword is the character you don't want.
02-04-2021 12:27 PM
Since I discovered that ST transformations were much faster than ABAP (of course, we can see the difference only with high volume), I always use them:
DATA : response_json TYPE string VALUE '[{"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489"},{"success":"true","msg":" valid","matnr":"PH X 15","matkl":"123433"}]'.
TYPES : BEGIN OF ty_response_line,
success TYPE string,
msg TYPE string,
matnr TYPE string,
matkl TYPE string,
END OF ty_response_line,
ty_response TYPE STANDARD TABLE OF ty_response_line WITH EMPTY KEY.
DATA response TYPE ty_response.
CALL TRANSFORMATION zst SOURCE XML response_json RESULT abapitab = response.
ZST transfo:
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
<tt:root name="ABAPITAB"/>
<tt:template extensible="deep-dynamic">
<array>
<tt:loop ref="ABAPITAB">
<object>
<tt:group>
<tt:cond frq="?">
<str name="success" tt:value-ref="SUCCESS"/>
</tt:cond>
<tt:cond frq="?">
<str name="msg" tt:value-ref="MSG"/>
</tt:cond>
<tt:cond frq="?">
<str name="matnr" tt:value-ref="MATNR"/>
</tt:cond>
<tt:cond frq="?">
<str name="matkl" tt:value-ref="MATKL"/>
</tt:cond>
</tt:group>
</object>
</tt:loop>
</array>
</tt:template>
</tt:transform><br>
02-04-2021 3:39 PM
Too funny.....and timely! Literally using this this morning for another need. haha
02-04-2021 2:23 PM
TYPES : BEGIN OF ty_data,
success TYPE string,
msg TYPE string,
matnr TYPE matnr,
matkl TYPE matkl,
END OF ty_data.
DATA : ls_data TYPE ty_data.
lt_data TYPE STANDARD TABLE OF ty_data.
DATA : lv_response TYPE string VALUE '[{"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489"},{"success":"true","msg":" valid","matnr":"PH X 15","matkl":"123433"}]',
/ui2/cl_json=>deserialize( EXPORTING json = lv_response
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
CHANGING data = lt_data ).
your string data is is json data so you can use json deserialize method.
After the deserialize your internal table wil be like this.
If you need json data you can use serialize method
DELETE lt_data2 INDEX 1.
/ui2/cl_json=>serialize( EXPORTING data = lt_data RECEIVING r_json = DATA(r_json) ).
02-05-2021 2:44 AM
02-04-2021 3:02 PM
I like that there are so many solutions here... I'll add another based on FIND and REGEX, just for fun.
DATA response TYPE string.
DATA json_table TYPE STANDARD TABLE OF string WITH EMPTY KEY.
response = '{"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489"},{"success":"true","msg":" valid","matnr":"PH X 15","matkl":"123433"}'.
FIND ALL OCCURRENCES OF REGEX '(\{[^\}]*\})' IN response RESULTS DATA(result_table).
LOOP AT result_table ASSIGNING FIELD-SYMBOL(<result_line>).
INSERT
substring( val = response off = <result_line>-submatches[ 1 ]-offset len = <result_line>-submatches[ 1 ]-length )
INTO TABLE json_table.
ENDLOOP.
out->write( json_table ).
Remember, you can almost always use regular expression to make things a bit harder to read. This thing works, but the other solutions are better... Son't use this one if you want readable code. (But if you do, catch the CX_SY_ITAB_LINE_NOT_FOUND exception for good measure... 🙂
02-04-2021 3:38 PM
NEVER use "regular expressions" and "fun" in the same sentence unless "not" is in there somewhere! hahaha
02-04-2021 4:40 PM