Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode JSON in ABAP before 7.02

Tags:

json

abap

As Horst Keller mentioned in his ABAP and JSON post, "with Releases 7.02 and 7.03/7.31 (Kernelpatch 116) JSON is supported natively in ABAP".

Appartently 7.02 in my case of too generic because the line below:

writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

returns the error: "The field CO_XT_JSON is unknown, but there is a field with the similar name CO_XT_XOP".

So is there any way to easily generate JSON?

Edit: Screenshot from SAP - Status

enter image description here

enter image description here

like image 279
Valentin Despa Avatar asked Apr 22 '13 18:04

Valentin Despa


5 Answers

About the class CL_TREX_JSON_SERIALIZER: I also used this class during developping a mobile sap application and I found the created JSON not being valid, thus I started googling and found this http://scn.sap.com/community/mobile/blog/2012/09/24/serialize-abap-data-into-json-format (which also explains how to create a valid JSON serializer). Validate your json with json lint http://jsonlint.com/ to see if it is valid.. otherwise, thats for sure, you get a lot of trouble in debugging why it doenst work and dont get the point that the serializer is corrupt. regards, zY

like image 87
dotchuZ Avatar answered Sep 28 '22 13:09

dotchuZ


take a look at the ZCL_MDP_JSON Library. You can parse/encode any JSON. So, it is best suited for JSON scenarios that requires flexibility.

It is easy to understand if you have used JSON in other languages. You only need to study methods of ZCL_MDP_JSON_NODE class once & look at the examples.

Here is an extended overview of the library: http://scn.sap.com/community/abap/blog/2016/07/03/an-open-source-abap-json-library--zclmdpjson

GitHub repo with examples directory: https://github.com/fatihpense/zcl_mdp_json

Disclaimer: I'm the author of the project. If you have questions, don't hesitate to contact me.

like image 43
fatihpense Avatar answered Sep 28 '22 12:09

fatihpense


Here is some code I wrote for ABAP data <-> JSON conversion some time ago before the new capabilities were included with ABAP (or maybe it was just an older system).

https://gist.github.com/mydoghasworms/2291540

Include the code in your ABAP source and use the method data_to_json of the class.

like image 22
mydoghasworms Avatar answered Sep 28 '22 12:09

mydoghasworms


A nice overview of custom ABAP <-> JSON serializers including yet another one can be found in this blog post

Most popular from my point of view is SE38's ZJSON-library which can be installed using SAPLINK (and which - in contrast to many others) has an explicit license attached to it: Apache 2.0

like image 39
Oliver J Avatar answered Sep 28 '22 14:09

Oliver J


If upgrading to a newer patch isn't an option in the short term, you can also use class CL_TREX_JSON_SERIALIZER to serialise objects to JSON. A little bit of a quick-and-dirty solution but it works well.

like image 39
mjturner Avatar answered Sep 28 '22 13:09

mjturner