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:
- Introducing JSON
- Blog of Horst Keller announcing the support of JSON
- More information from Horst Keller in this blog
- Blog from Kerem Koseoglu about useful classes in connection with JSON
- Project “zJSON” by Uwe Fetzer
- Project “JSON2ABAPType” by Łukasz Pęgiel
- SAP Online Help about JSON
Best regards and thanks for reading
Michael
Interesting, I've never really worked on JSON start to finish. Now I've got an excellent reason to do it.
That's the point. There are a lot of interesting technologies I've never used but they make life easier. Looks like a long journey for us 😉
It should be a lot of fun!
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
Hi Łukasz,
thanks for the note and your effort to write another nice ABAP tool to make our life easier. I added your project to the list above.
Best regards
Thanks Michael
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.
Hi Jelena,
of course you can POST JSON to an OData service (and read with URL parameter $format=json) 🙂
JSON for Dummies: http://json.org/
Hi Uwe,
thanks for the link, I added it to the blog.
Hi Michael,
very timely blog as i found myself working with json files quite extensively recently. my current toolbox is:
http://objgen.com/
https://json-csv.com/
and a text editor + Excel365 Power Query
for smaller payloads they all work fine, but for realistic work loads we need something more serious like your coding.
thank you and looking for further updates.
Regards, greg
I am happy if the blog helps you 🙂 I wish you success!
Really Interesting. Jelena , happy that you have already started to catch. Motivating Intro.