Class /UI2/CL_JSON and special names

Reading time:

1–2 minutes

Dear community, the /UI2/CL_JSON class is very popular for converting an ABAP data structure to JSON format. I use it, too, especially when developing on an older SAP NetWeaver stack.

I recently encountered three problems, all of which could be solved using the same method. First, a description of the problems, all of which concerned the name of the name/value pair:

  1. The name contained a namespace, separated from the actual identifier by a colon. Example: “awesomeCompany:Name.”
  2. The name had more than 30 characters.
  3. A special notation was chosen for the name, not a general naming convention like “camel case.”

All three of the above points are permissible with regard to the name of a name/value pair according to the JSON convention. This is because the name is a string. Within the string, anything, almost anything, is possible.

Unfortunately, this is precisely what causes problems when automatically converting an ABAP structure using the SERIALIZE method of the /UI2/CL_JSON class. This method can only translate component names of a structure up to 30 characters long, because longer component names are not allowed.

A colon within the name cannot be automatically displayed because it is also not allowed in the component name of an ABAP structure.

Unusual spellings are also not possible because they are not part of the component name of an ABAP structure. Case sensitivity is irrelevant.

The solution is the import parameter NAME_MAPPINGS of the SERIALIZE method. This parameter passes a table of manual mappings. This contains the original component name of the ABAP structure, along with the corresponding JSON representation as a string.

While all of the problems described above can be solved this way, unfortunately, a corresponding manual mapping is also required – this can quickly become quite complex.

With that in mind, happy JSON generation

Michael