Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to split string to itab without remove character in string.

jandrivay
Participant
0 Kudos

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"} <--

1 ACCEPTED SOLUTION

 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) ).
9 REPLIES 9

mateuszadamus
Active Contributor
0 Kudos

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,
Mateusz

Patrick_vN
Active Contributor
0 Kudos

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.

Sandra_Rossi
Active Contributor

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>

Too funny.....and timely! Literally using this this morning for another need. haha

 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) ).

Hi, Hasan.

Thanks for your comment, my problem solved. 🙂

joltdx
Active Contributor

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... 🙂

ChrisSolomon
Active Contributor

NEVER use "regular expressions" and "fun" in the same sentence unless "not" is in there somewhere! hahaha

joltdx
Active Contributor
0 Kudos

Hahaha ¯\_(ツ)_/¯