Skip to Content
Technical Articles

ABAP and JSON

Dear Community, as a programmer I’m always happy when data is structured. That makes working with it easier. Over the years I have been using different formats such as XML for example.

A few years ago I heard about JSON for the first time. Unfortunately I did not deal directly with the format at the time. I did that now and would like to share my experiences with you 🙂

The basics of JSON can be found on Wikipedia. Based on the comparison there with other formats, you can decide quite quickly whether you like the representation of data in JSON. It is definitely shorter than XML.

I like the representation. Somehow it fits more to my thinking as a programmer than XML. But that may be because I used to develop in other programming languages and I like the curly braces 😉

To get started with the topic “ABAP and JSON”, I did a little internet research. And surprisingly found that help.sap.com offers extensive help. Because JSON is directly supported by ABAP using the command CALL TRANSFORMATION. And this command is well known when you are working with XML. The support was also announced by Horst Keller in a blog.

In order to be able to work with JSON in ABAP under laboratory conditions, I have written two small examples. The examples are available via abapGit on GitHub. The goal was to serialize and deserialize different types of data.

For this purpose I used the asJSON representation (canonical JSON representation). Especially handy in this context: You can serialize or deserialize the instance of a class with all its data. This possibility also exists for asXML. From my point of view, this simplifies the work with data in an object-oriented way very much.

If you have individual requirements for the processing of JSON data, you can also create an XSL or Simple Transformation. Alternatively, you can work with classes like /ui2/cl_json and /ui5/cl_json_parser (check this blog by Kerem Koseoglu).

My example is primarily about serializing and deserializing the class ZJSON_CL_EXAMPLE. The class has data with different data types like string, abap_bool, integer, packed with decimals, a structure and a table.

The report ZJSON_PLAYGROUND performs serialization and deserialization for test purposes. This is easy to trace by debugging.

For serialization, data and its values is set in report ZJSON_PLAYGROUND. The situation is different with the deserialization. To experiment as quickly and easily as possible with different data and data types, I have written a small JSON provider in the form of the class ZJSON_CL_EXAMPLE_PROVIDER, which reads JSON data from an ABAP include. So a quick change to all parts of my tests, namely the ABAP development objects and the JSON data, was very easy in Eclipse with ABAP Development Tools.

Here is the content of the include with JSON test data. The comment characters are removed by using the class ZJSON_CL_EXAMPLE_PROVIDER.

*{
* "OREF":
* {
*  "%ref":"#o11"
* },
* "%heap":
* {
*  "o11":
*  {
*   "%type":"cls:ZJSON_CL_EXAMPLE",
*   "%val":
*   {
*    "ZJSON_CL_EXAMPLE":
*    {
*     "TEXT_VALUE" : "Hello World!",
*     "BOOLEAN_VALUE" : true,
*     "INTEGER_VALUE" : 1024,
*     "NEGATIVE_INTEGER_VALUE" : -256,
*     "PERCENTAGE_VALUE" : 99.99,
*     "NULL_VALUE" : null,
*     "STRUCTURED_VALUES" : { "FIELD1" : "Test", "FIELD2" : "Test2" },
*     "TABLE_LINES" :
*     [
*         { "LINE" : "1", "VALUE" : "Testline 1" },
*         { "LINE" : "2", "VALUE" : "Testline 2" }
*     ]
*    }
*   }
*  }
* }
*}

You can validate the pure JSON data by using an online validator like this one. Don’t forget to remove the comment characters.

That’s all for now. After working in the lab, I have to proof where I can use it in my daily practice 🙂 Please feel free to make your own tests and share your experiences.

Finally, an overview of interesting links to get started:

 

Best regards and thanks for reading

Michael

9 Comments
You must be Logged on to comment or reply to a post.
  • Hi Michael,

    please take a look also on my project JSON2ABAPType which can be useful to create the ABAP types from JSON structure. It’s mainly based on usage of  /UI2/CL_JSON which is very nice and powerful class for JSON.

    Cheers

  • Nice collection of external links! 🙂

    I’m just now jumping on the JSON bandwagon (or rather the wagon has left already and I’m running behind clutching my chest and wheezing “wait for me!” 🙂 ). Possibly a stupid question: could this be used, say, when calling an OData service from ABAP (with POST)? For example, I adapted an example from this blog but it just brutally appends text to a string and that’s not great.

    Also I’d really love to read some “JSON for dummies” blog on SCN. As you said, there is a lot of content online but that’s actually the problem. It’s like “drinking from a fire hose”. Would be nice if someone could break it down in simple terms and with SAP-specific examples.