Complete JSON utility class example

Following my previous post, where I showed a solution for translating JSON to an ABAP data structure, I am just posting an example of a class that will allow you to do both (ABAP to JSON and vice versa).

The code is in this gist: https://gist.github.com/2291540

Again, you too may find it more convenient to put this into a repository class, but a local class is just easier to put into a gist :-)

You may also be interested in:

  • No Related Posts
  • Rüdiger

    Martin,

    this JSON builder is the natural counterpart of the parser that you already had presented.

    My main need for JSON is when using a library like Open Flash ( http://teethgrinder.co.uk/open-flash-chart-2/ ), and an HTTP request handler to provide that component with the appropriate data. In this case, the identifiers (the hash keys) are usually mixed case, and the structures are deeply nested. I am not sure whether your approach can handle mixed case key words. But even if it can, it would require defining a deep DDIC structure which is an exact counterpart of the JSON data format.

    For these reasons, I have designed a JSON builder working with a generic carrier data type (ZUT_DATA), as the parser did. Here is the description of the builder: http://ruediger-plantiko.blogspot.com/2011/09/ein-json-builder-in-abap.html At the core of the builder, there is a transformation zabap2json ( http://bsp.mits.ch/code/xslt/zabap2json ), since I found XSLT very appropriate for dealing with this kind of recursions (regarding string manipulation or even computations, it is surely not the champion…).

  • Martin

    Thanks, Rüdiger. You are right, my solution deals only with an existing structure (though not necessarily a DDIC structure, it can be defined in the current program), and now I also understand the purpose of your solution better.
    For my project, the requirement was to have a web application that communicates with the SAP system as backend, and is therefore dependent on the data structures defined in SAP. As such, SAP being the “master”, it is of greater concern to me to be able to translate to and from an ABAP structure (and hence the nuances we discussed previously regarding boolean values also).
    You are also right that case is not important in my solution, as it is always mapped to upper case for the sake of ABAP fields.

  • Martin

    Another solution I have seen for mapping Javascript to ABAP is using CL_JAVA_SCRIPT, which I guess you can use to read values from arbitrary pieces of JSON (though probably smaller ones for performance reasons, but it is a good idea too).
    Anyway, it is nice to see that there is quite a bit of interest in this kind of solution after all. Hopefully SAP will add better support for it in future.

  • rplantiko

    Martin,

    be careful with CL_JAVA_SCRIPT, however. Although I am a fan of this built-in interpreter, including its binding features to ABAP objects and data, I was shocked after reading in the documentation of our new SAP_BASIS 702 that the class is obsolete and is going to be eliminated *with no substitute* !!!

    And this is in times where other languages like Java embrace the option of having a built-in dynamic languages framework (in times where everybody speaks of fluid, expressive syntax, DSL, and so on). It’s a shame.

    Kind regards,
    Rüdiger

  • Martin

    Hello Rüdiger, thanks for the info. Yes, that is a bit of a shocker, and very unfortunate, since one would expect that this should form an important part of support for web-based interfaces and dynamic data exchange.

  • http://ruediger-plantiko.blogspot.com Rüdiger

    Hello Martin,

    news from the SAP basis: Finally, there is built-in JSON support. They implemented it in the kernel, available with the CALL TRANSFORMATION statement.

    This deprecates all our ABAP-implementations of JSON parsers and JSON builders! Using the “canonical” JSON representation of ABAP data, parsing and building JSON reduces to one-liners with “CALL TRANSFORMATION ID …”

    See here Horst Keller’s announcement:

    http://scn.sap.com/people/horst.keller/blog/2013/01/07/abap-and-json

    And my discussion of a live example

    http://scn.sap.com/community/abap/blog/2013/01/24/developing-a-rest-api-in-abap#Using_ABAPs_BuiltIn_JSON_Converter

    Regards,
    Rüdiger