Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Michael_Keller
SAP Champion
SAP Champion
20,577
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
12 Comments
Labels in this area