FUNCTION Z_JSON_FORMATER_VALUE .
*"--------------------------------------------------------------------
*"*"Interface locale :
*" CHANGING
*" REFERENCE(STR) TYPE STRING
*"--------------------------------------------------------------------
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf IN str WITH '\n'.
* OH OH... en entrée il peut déjà y avoir \" et dans ce cas il deviendrait \\" donc on défait et refait.
REPLACE ALL OCCURRENCES OF '\"' IN str WITH '"'.
REPLACE ALL OCCURRENCES OF '"' IN str WITH '\"'.
ENDFUNCTION.
here is my definition of tab_struct. I hope that will help you. I also made some changes to the code. If you're interested, you are welcome to contact me.
Best ragards
Roman Albisser
TYPES: BEGIN OF tab_struct,
type TYPE string,
fieldname TYPE string,
END OF tab_struct.
I read and tried your SDN code on JSON in ABAP.
Z_JSON_OUT is working fine.
Z_JSON_IN I am not able to use as Z_JSON_DEFORMATER_VALUE is missing.
A few examples how to use the 2 FMs would help all who plan to use.
Calling them Z_JSON_ENCODE and Z_JSON_DECODE would
align these with general naming convention by others.
In Z_JSON_OUT too has a minor glitch - ISTRUC TYPE ZGY_0004 OPTIONAL
I deleted this reference and it seems not to affect functionality.
I plan to use this in PHP and FLEX3 integration.
The code is compatible only to ECC6 and not 4.6 4.7
With lots of 4.n SAP installations still around 4.n compatible code would help.
FUNCTION Z_JSON_DEFORMATER_VALUE.
*"----------------------------------------------------------------------
*"*"Interface locale :
*" CHANGING
*" REFERENCE(STR)
*"----------------------------------------------------------------------
DATA: elemdescr TYPE REF TO cl_abap_elemdescr.
* Description of element
elemdescr ?= cl_abap_elemdescr=>describe_by_data( STR ).
CHECK elemdescr->TYPE_KIND <> 'P' and elemdescr->TYPE_KIND <> 'I'.
REPLACE ALL OCCURRENCES OF '\n' IN str WITH cl_abap_char_utilities=>cr_lf.
REPLACE ALL OCCURRENCES OF '\"' IN str WITH '"'.
ENDFUNCTION.
Here is a sample of Z_JSON_OUT :
For a table :
DATA i_countries TYPE TABLE OF T005T.
DATA w_json type string.
SELECT * INTO CORRESPONDING FIELDS OF TABLE i_countries FROM t005t
WHERE spras = sy-langu.
CALL FUNCTION 'Z_JSON_OUT'
IMPORTING
json = w_json
TABLES
itab = i_countries.
for a Structure :
DATA w_country TYPE T005T.
DATA w_json type string.
Some characters outside ASCII create issues in PHP JSON DECODE reading ABAP JSON Encoded data
This might be an issue of SAPRFC for PHP as well
In our material descriptions we have many cases of
a) invisible control characters
b) E e with accent and reverse accent
c) Microsoft Word Double quotes and single quotes and hyphen
These are very deceptive as they look like what they are not
I have spaced out or replaced such characters with closest ASCII .
For JSON technology to be safe I have spaced out : \ []{}
Examples
CADBURY ÉCLAIR CRUNCH 484GM
VEDCHINESE F/VASE 10*"* "" "
HEINZ CRÈME CARAMEL 170G - FRM6MNTHS
Rev Crème de La Chrome Liquid Lipcolor
Can*'*t Take a Joke '' '
Users of ABAP JSON ma need to adapt z_json_formater_value to suit individual needs.
12 Comments
Unknown User (101h13k7b)
Hi,
This is exactly what I was looking for! We're considering using JSON as a generic interchange format for a project, but need ABAP support
You seem to have left out the definition of the function Z_JSON_FORMATER_VALUE. Could you please add it?
I look forward to trying this out!
Regards,
Thorsten
Unknown User (v084fux)
Here it is :
Former Member
Z_JSON_DEFORMATER_VALUE is also missing
Unknown User (d5891hn)
HI ,
Great example!
i try to use your example but i can't find the type of tab_struct,also
CASE w_struct-type.
maybe u can help.
Regards
Unknown User (mz4e64t)
Hi,
I'm very much interested in looking forward to this approach.
I've used this in one of my PHP project but now i want to use it in ABAP.
i tried finding the type tab_struct but i didn't get any ..
Can you direct me ??
Regards
Sandeep Solanki
Unknown User (102a3zaof)
Hello,
here is my definition of tab_struct. I hope that will help you. I also made some changes to the code. If you're interested, you are welcome to contact me.
Best ragards
Roman Albisser
Anonymous
ABAP JSON is unique and outstanding effort.
I read and tried your SDN code on JSON in ABAP.
Z_JSON_OUT is working fine.
Z_JSON_IN I am not able to use as Z_JSON_DEFORMATER_VALUE is missing.
A few examples how to use the 2 FMs would help all who plan to use.
Calling them Z_JSON_ENCODE and Z_JSON_DECODE would
align these with general naming convention by others.
In Z_JSON_OUT too has a minor glitch - ISTRUC TYPE ZGY_0004 OPTIONAL
I deleted this reference and it seems not to affect functionality.
I plan to use this in PHP and FLEX3 integration.
The code is compatible only to ECC6 and not 4.6 4.7
With lots of 4.n SAP installations still around 4.n compatible code would help.
Looking forward to your kind help.
Regards
-jnc
Unknown User (v084fux)
here is Z_JSON_DEFORMATER_VALUE:
Here is a sample of Z_JSON_OUT :
For a table :
for a Structure :
About SAP 4.6 - 4.7, i don't have access to it so i can't help you out.
sorry
Anonymous
Thanks Quentin. For your most prompt response.
I wrote the following test program and it worked fine for many tables.
The version for EKBE is pasted below. Shopws rubustness of BOTH IN & OUT.
*&---------------------------------------------------------------------*
*& Report YJNCTEST08
*&---------------------------------------------------------------------*
REPORT yjnctest08.
DATA: gt_ekbe TYPE STANDARD TABLE OF ekbe.
DATA: js_ekbe TYPE STANDARD TABLE OF ekbe.
DATA: myjson TYPE string.
DATA: linecount TYPE i.
START-OF-SELECTION.
SELECT *
INTO TABLE gt_ekbe
FROM ekbe
UP TO 10 ROWS.
CALL FUNCTION 'ZPHP_JSON_OUT'
IMPORTING
json = myjson
TABLES
itab = gt_ekbe.
WRITE: /, myjson.
CALL FUNCTION 'ZPHP_JSON_IN'
EXPORTING
json = myjson
TABLES
itab = js_ekbe.
MOVE lines( js_ekbe ) TO linecount.
WRITE: /, linecount.
IF js_ekbe = gt_ekbe.
WRITE: /, 'OUT IN Match SUCCESS!' .
ELSE.
WRITE: /, 'OUT IN Match FAILED!' .
ENDIF.
Anonymous
I checked that Z_JSON_IN alone is not 4.7 compatible as it uses ECC6 ABAP syntax in parts.
The most important Z_JSON_OUT is 100% 4.7 compatible and works fine.
The Quentin routines never fail. But PHP json_decode cannot handle very large strings.
It is not worrying me as in Portal or Flex3 contexts data will be small.
Anonymous
Some characters outside ASCII create issues in PHP JSON DECODE reading ABAP JSON Encoded data
This might be an issue of SAPRFC for PHP as well
In our material descriptions we have many cases of
a) invisible control characters
b) E e with accent and reverse accent
c) Microsoft Word Double quotes and single quotes and hyphen
These are very deceptive as they look like what they are not
I have spaced out or replaced such characters with closest ASCII .
For JSON technology to be safe I have spaced out : \ []{}
Examples
CADBURY ÉCLAIR CRUNCH 484GM
VEDCHINESE F/VASE 10*"* "" "
HEINZ CRÈME CARAMEL 170G - FRM6MNTHS
Rev Crème de La Chrome Liquid Lipcolor
Can*'*t Take a Joke '' '
Users of ABAP JSON ma need to adapt z_json_formater_value to suit individual needs.
<!-- /* Font Definitions */ @font-face
@font-face
/* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal
span.l1s521
span.l1s311
span.l1s321
span.l1s331
.MsoChpDefault
@page Section1
div.Section1
-->
FUNCTION z_json_formater_value .
*"----------------------------------------------------------------------
*"*"Local Interface:
*" CHANGING
*" REFERENCE(STR) TYPE STRING
*"----------------------------------------------------------------------
DATA: len TYPE i,
off TYPE i,
onebyte TYPE c.
* : \ [ ]
ÉEÈEÊEËEèeéeêeëe#"#"#'#'#-
DATA: maptab(38) TYPE x VALUE '3A205C205B205D207B207D20C945C845CA45CB45E865E965EA65EB659322942291279227972D'.
FIELD-SYMBOLS:
<tb> TYPE x,
<tc> TYPE c.
ASSIGN maptab TO <tb> CASTING.
ASSIGN <tb> TO <tc> CASTING.
TRANSLATE str USING <tc>.
len = STRLEN( str ).
off = 0.
DO len TIMES.
onebyte = str+off(1).
IF onebyte < ' ' OR onebyte > '~'.
REPLACE SECTION OFFSET off LENGTH 1 OF str WITH ` `.
ENDIF.
ADD 1 TO off.
ENDDO.
REPLACE ALL OCCURRENCES OF '"' IN str WITH '\"'.
ENDFUNCTION.
Unknown User (v084fux)
the last version can be found here : ZJON google code
regards
Quentin