Skip to end of metadata
Go to start of metadata

Welcome to JSON & ABAP project. This project aims to develop tools to convert ABAP objects / XML to JSON format and parse JSON to ABAP objects / XML. We request all memebers to come out with their inputs.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.More Info

Sample JSON format (10 records from SFLIGHT table)[

[{"MANDT":023,"CARRID":null,"CONNID":0000,"FLDATE":"0000-00-00","PRICE":0.0,"CURRENCY":null,"PLANETYPE":null,"SEATSMAX":0,"SEATSOCC":-18,"PAYMENTSUM":0.0,"SEATSMAX_B":0,"SEATSOCC_B":0,"SEATSMAX_F":0,"SEATSOCC_F":0},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-02-16","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":348,"PAYMENTSUM":189062.87,"SEATSMAX_B":31,"SEATSOCC_B":30,"SEATSMAX_F":21,"SEATSOCC_F":19},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-03-16","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":346,"PAYMENTSUM":188559.57,"SEATSMAX_B":31,"SEATSOCC_B":29,"SEATSMAX_F":21,"SEATSOCC_F":20},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-04-13","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":356,"PAYMENTSUM":193622.26,"SEATSMAX_B":31,"SEATSOCC_B":29,"SEATSMAX_F":21,"SEATSOCC_F":21},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-05-11","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":352,"PAYMENTSUM":192996.18,"SEATSMAX_B":31,"SEATSOCC_B":30,"SEATSMAX_F":21,"SEATSOCC_F":21},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-06-08","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":355,"PAYMENTSUM":194539.96,"SEATSMAX_B":31,"SEATSOCC_B":31,"SEATSMAX_F":21,"SEATSOCC_F":21},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-07-06","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":354,"PAYMENTSUM":192420.92,"SEATSMAX_B":31,"SEATSOCC_B":30,"SEATSMAX_F":21,"SEATSOCC_F":20},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-08-03","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":354,"PAYMENTSUM":195136.12,"SEATSMAX_B":31,"SEATSOCC_B":31,"SEATSMAX_F":21,"SEATSOCC_F":21},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-08-31","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":356,"PAYMENTSUM":192962.37,"SEATSMAX_B":31,"SEATSOCC_B":29,"SEATSMAX_F":21,"SEATSOCC_F":20},
 {"MANDT":023,"CARRID":"AA","CONNID":0017,"FLDATE":"2005-09-28","PRICE":422.94,"CURRENCY":"USD","PLANETYPE":"747-400","SEATSMAX":385,"SEATSOCC":-18,"PAYMENTSUM":0.0,"SEATSMAX_B":31,"SEATSOCC_B":0,"SEATSMAX_F":21,"SEATSOCC_F":0}] 

Why JSON with ABAP?

Already services like Yahoo Google Del.icio.us Flickr provides alternate output method to output webservice results in JSON format as the data interchange format is light weight. May be more webservice providers will come up with this option and with this tool we can be ready to handle them. Please note that this project doesn't get into the disucssion of is JSON better than others (XML).

What to expect from this project?

1. ABAP XML to JSON text converter. ( we are currently using XSLT program from XML to JSON XSLTto convert ABAP XML to JSON)

2. ABAP objects viz., internal table, variable, etc to JSON format

3. JSON to ABAP XML

4. JSON to ABAP Object viz., internal table variable, etc

At a later stage we could also think of providing JSON output from ABAP webservices.

These tools will be in the form of Function modules and there will be two versions of each of them , one which can run in all WAS versions and the other which will run from WAS7.0 onwards (using Regular Expressions)

 ZJSON

ZJSON is a  function group that include all the function module we use here with Flex and SAP, and you can find the lastest updated code over here : http://code.google.com/p/zjson/

 Sample report :


 

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*&---------------------------------------------------------------------*
*& Report  Y_TEST_JSON
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT y_test_json.
********** DATA *************
DATA w_json_out TYPE string.
DATA w_json_out_tab TYPE string.
DATA w_json_string TYPE string.
DATA : BEGIN OF struc1,
        pernr TYPE persno,
        nachn TYPE nachn,
       END OF struc1.
DATA struc2 LIKE struc1.

DATA : BEGIN OF w_json_in ,
     objid   TYPE hrobjid,
     histo(1)    TYPE c,
     date   TYPE d,
 END OF  w_json_in.
 
DATA : BEGIN OF itab1 OCCURS 0,
        pernr TYPE persno,
        nachn TYPE nachn,
       END OF itab1.
DATA itab2 LIKE itab1 OCCURS 0 WITH HEADER LINE.

DATA w_json_compil TYPE zgs_0004.
DATA i_json_compil TYPE zgy_0004.
DATA w_json_compil_string TYPE string.
*********** END OF DATA ********
************** JSON IN ***********
w_json_string  = '{"date":"20080221","histo":"","objid":"50215887"}'.

CALL FUNCTION 'Z_JSON_IN'
  EXPORTING
    json  = w_json_string
  IMPORTING
    struc = w_json_in.

*************JSON OUT **************
struc1-pernr = '00010003'.
struc1-nachn = 'Jacko'.
CALL FUNCTION 'Z_JSON_OUT'
  EXPORTING
    struc = struc1
  IMPORTING
    json  = w_json_out.
 
CALL FUNCTION 'Z_JSON_IN'
  EXPORTING
    json  = w_json_out
  IMPORTING
    struc = struc2.
WRITE : 'STRUC'.
NEW-LINE.
WRITE : struc2-pernr, struc2-nachn.
SKIP.

************* JSON OUT TABLE **************

itab1-pernr = '05114491'.
itab1-nachn = 'John'.
APPEND itab1 TO itab1.
itab1-pernr = '051125681'.
itab1-nachn = 'Cindy'.
APPEND itab1 TO itab1.
CALL FUNCTION 'Z_JSON_OUT'
  IMPORTING
    json = w_json_out_tab
  TABLES
    itab = itab1.

CALL FUNCTION 'Z_JSON_IN'
  EXPORTING
    json = w_json_out_tab
  TABLES
    itab = itab2.
WRITE : 'ITAB'.
NEW-LINE.
LOOP AT itab2.
  WRITE : itab2-pernr, itab2-nachn.
  NEW-LINE.
ENDLOOP.
SKIP.
************ JSON COMPIL *****************
w_json_compil-id = 'STRUC'.
w_json_compil-tx = w_json_out.
APPEND w_json_compil TO i_json_compil.
w_json_compil-id = 'TAB'.
w_json_compil-tx = w_json_out_tab.
APPEND w_json_compil TO i_json_compil.
CALL FUNCTION 'Z_JSON_COMPIL_JSONS'
  IMPORTING
    json  = w_json_compil_string
  TABLES
    jsons = i_json_compil.

WRITE : 'COMPIL'.
NEW-LINE.
WRITE  w_json_compil_string.
SKIP.
*******************************************



7 Comments

  1. Pretty neat!  Where's the funtion module code? Did I overlook it?

  2. Unknown User (ggxjewg)

    Which SAPLink plugins have you used? Seems like I'm having trouble installing them.

  3. Thats is absolutely great, thanks for posting.

    I have developed a (hopefully) nice SICF generic service for extracting data in JSON based on your development...

    Kinda good old RFC_GETDATA... :D

    Kinda backdoor ahahah

    Cheers

    Alex

  4. Hi,

    I saw a CL_TREX_JSON_SERIALIZER class that performs serialization ABAP -> JSON.
    Do you know if there are functional gaps with ZJSON? How does it compare in terms of performance?

    Thx !

     [Update 17.04.2013] Seems that this class does not generate JSON "properly". It cannot be used by external tools that are strict with some formatting rules

    Best regards,
    Guillaume

  5. Hi,

    WIth Releases 7.02 and 7.03/7.31 (Kernelpatch 116) JSON is supported natively in ABAP by the following features:

    • JSON-XML is a special XML format that enables JSON data to be described using an XML representation. A new format, IF_SXML=>CO_XT_JSON, has been added to the sXML Library, which enables to handle JSON using JSON-XML.
    • The canonical JSON representation asJSON defines a mapping between ABAP types and JSON. This is used in serializations and deserializations using the identity transformation ID.
    • JSON data can be specified in various forms as an XML source in the statement CALL TRANSFORMATION and a JSON writer can be specified as target. The identity transformation ID supports JSON by using asJSON.

    Example:

     DATA text TYPE string VALUE `Hello JSON, I'm ABAP!`.
     DATA writer TYPE REF TO cl_sxml_string_writer.
     DATA json TYPE xstring.

     "ABAP to JSON
     writer = cl_sxml_string_writer=>create(
       type = if_sxml=>co_xt_json ).
     CALL TRANSFORMATION id SOURCE text = text
                            RESULT XML writer.
     json = writer->get_output( ).

     "JSON to ABAP
     text = `{"TEXT":"Hello ABAP, I'm JSON!"}`.
     CALL TRANSFORMATION id SOURCE XML text
                            RESULT text = text.

  6. For a earlier mobile project I had implemented an object oriented ABAP library to handle JSON data like known in javascript, java,...Some month ago I have ported this stuff to the /CEX/ namspace. It's a SAP transport request without Z* objects. You can find and try it for free at Sourceforge: http://sourceforge.net/projects/json4sapnw/.

    This can be used for a more generic way to process and create json data. I have used the library for creating data for javascript pages (SE80/BSP) or to get external JSON calls within a SAP BSP handler. The JSON messages can be large for transporting images or other binary objects. Some examples can be found at the sourceforge page.

    Enjoy...

  7. Former Member

    Hi..Manish,

    Great information on JSON and ABAP .

    I have concern as below:

    i have consumed restful webservice in ABAP .

    I am receiving response in variable like  LV_RESPONSE = {"status":"Failed"}.

    So how can i convert the lv_response to ABAP structure  like       field name = status and field value = Failed.

    If i received table data from json..how can i do it with ABAP internal table..

     

    Kindly advice me...

     

    Regards,

    Mahesh Reddy