Skip to Content
[JSON | http://json.org/], as stated on its website, is a lightweight data interchange format. Surprisingly, despite JSON popularity these days, there is not much material to be found on the subject of XML vs. JSON comparison (usability, overhead, performance). Hopefully this will change soon with JSON broadening adoption as a standard  (http://www.cio.de/news/cio_worldnews/2217519/index3.html).At some point, however, I came across an interesting code demonstrating how to use regular expressions in pre-7.0 NW environment, more precisely from SAP R/3 Enterprise (4.70) onwards, using internal JavaScript engine CL_JAVA_SCRIPT ({code:html}ABAP: Using Regex / Regular Expressions in a Pre-Netweaver Release{code}). As JSON is nothing else than a subset of the object literal notation in JavaScript, I switched to thinking of interpreting JSON in the internal NetWeaver JavaScript engine and binding the result to ABAP… So was smartJSON born.Due to the logic how ABAP data are bound to their JavaScript representation (see the documentation for the method {code:html}CL_JAVA_SCRIPT->BIND( ){code}), it may be useful to use an intermediate interface structure in ABAP with string / character (and also possibly packed) types only, very much like a BAPI structure. Data mapped onto the fields of the interface structure can then be converted to / from their actual ABAP data types. It may make sense to use ISO format e.g. for date values in the intermediate ABAP structure for the sake of compatibility with other systems. The report ZSMARTJSON_DEMO demonstrates this as well.  h2. The architecture of the smartJSON library  The library entry point is an ABAP OO front-end ZCL_SMARTJSON_ABAP_PROCESSOR, written in a service-oriented fashion. Actual library code is written in JavaScript and serves the purpose of mapping the data between ABAP proxy object and native JavaScript variables. Parsing and generation of JSON string from a JavaScript variable is done using JSON utility from MooTools framework  (http://mootools.net/). Although it is possible to create a JavaScript object just by assigning a JSON string to a JavaScript variable, using an external parser makes it possible to validate the JSON string. JavaScript source loading has been implemented separately from the main library class. This is due to the fact that scripts may be stored in various ways (in DB, in the file system, in a web server and so on). Default smartJSON library installation package included with this blog uses an application server file system based solution (it was easier to transport using SapLink). However, any kind of storage will be fine as long as functionalities of the class ZCL_SMARTJSON_SCRIPT_LOADER are preserved.  h2. Components of the installation package and deployment procedure  Installation package can be downloaded from the smartJSON project page  (http://code.google.com/p/smartjson/) and contains the following objects: – SAPLink  (http://code.google.com/p/saplink/) nugget file NUGG_SMARTJSON-1.0.nugg. – JavaScript sources for smartJSON library and JSON parser / generator (SMARTJSON.js, JSON.js)  A working SAPlink is required for the installation. I used SAPlink version 0.1.4 for nugget creation. h3. Installation of ABAP OO components Nugget file NUGG_SMARTJSON-1.0.nugg must be imported using SAPLink. It is advisable to create a package ZSMARTJSON manually beforehand as all the objects belong to the ZSMARTJSON namespace. After activation of all the imported objects, JavaScript sources must be imported using report ZSMARTJSON_SCRIPT_EDITOR. h3. Installation of the JavaScript source files Two JavaScript source files must be installed: ** SMARTJSON – actual library code Run the report ZSMARTJSON_SCRIPT_EDITORsmartJSON script editor selection screenType script name : “JSON” in capital letters and run the report (F8)smartJSON script editor interfaceLoad file JSON.js from unpacked installation packageLoad JSON.js source fileClick “Save”.Save JSON source file
To report this post you need to login first.

12 Comments

You must be Logged on to comment or reply to a post.

    1. Former Member Post author
      Unfortunately there appears to be a problem with SAPLink. I had a similar issue despite the fact that the method implementation code IS there, in the nugget XML. Only that it is not imported, for whatever reason.
      A temporary solution is to copy source code manually from the nugget XML and paste it into the method.

      Yes I have seen Your project but the requirements which I set were closer to some kind of a generic transformation of arbitrary JSON data to arbitrary ABAP.

      At the moment I am working on an integration scenario with MS BizTalk, based upon SAP ICF on the one side and BizTalk WCF adapter on the other. The project will be using smartJSON library on the SAP side.

      (0) 
    2. Torsten Schubert
      I had the same problems with missing redifinition of abstract methods. I just started the import of SAPLink twice, then it worked well.

      I got the demo running also.

      (0) 
  1. Vikas Lamba
    Hi,

    I wanted to use your code for parsing JSON in ABAP but when I try to install the nugget it dumps while parsing the class ZCL_SMARTJSON_SCRIPT_LOADER/or whatever has to be installed after this.

    Are you aware of this issue? And if so can you help me?

    (0) 
  2. Former Member
    Very helpful to me. Works good and I use it to interact with and external REST web service.

    My main pain point now is that the REST service I work with uses “CamelCased” property names. So the parsing from JS to ABAP works fine, but the other direction is worse. Any ideas where to affect the mapping behavior?, i.e where a conversion from abap “FOO_BAR” field name to JavaScript “fooBar” can take place?

    (0) 
    1. Former Member Post author
      My best suggestion would be to create interface structures in ABAP and do all the mapping on the ABAP side (like in a typical BAPI scenario).
      An alternative would be to do the mapping on the JavaScript side but it is not something I would advise, really.
      I am thinking of a new version where script names during installation would no longer be obligatory. It is quite easy to accomplish, in fact, but I am not sure when I find time for this.
      (0) 
  3. Holm Kirschgens
    Hi,

    first of all: Looks good and may be very useful. But what’s about Code Injection? I’m afraid it’s possible to execute Javascript code on the server side with an appropriately prepared JSON string. In particular the script doesn’t run in a sandbox but it can call class methods in the ABAP stack (methods of classes that have been bound to the JS-Interpreter).

    (0) 
    1. Former Member Post author
      Hi Holm,
      This is true. This version of JSON processor has been built as a Proof of Concept rather than anything else though. E.g. the performance (in comparison e.g. with native ABAP<->asXML conversion) is, to put it gently, not exactly very impressive.
      At the moment I am drafting an ABAP OO-based JSON processor working without help of the JavaScript engine (native processing has future!). Obviously, the best one would be an interpreter implemented in kernel but I don’t think we’re getting such a tool soon. Unfortunately, JSON/REST and their vast potential suffer from serious underexposure considering humongous investments into SOAP…
      Kind regards
      Wiktor Nyckowski
      (0) 

Leave a Reply