Convert JSON to ABAP Internal Table and ABAP Data to JSON Format

Convert JSON and ABAP, List of The most important JSON ABAP Classes in SAP

JSON is a more and more used when dealing with communication between SAP System and different Information System Landscape.

Here we will start with a fast definition for JSON and JSON Data.
Then we will explain 3 different ways/Alternatives to convert JSON Data to ABAP Internal Table and ABAP Data to JSON. 

JSON vs ABAP Data

What’s JSON ?

JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard. JSON defines a small set of formatting rules for the portable representation of structured data. (source)

This document removes inconsistencies with other specifications of JSON, repairs specification errors, and offers experience-based interoperability guidance.

The handling of JSON data in ABAP is based on the following principles:

  • A JSON-XML format maps JSON data to XML.
  • XML readers and XML writers in sXML Library support JSON-XML and can be used as parsers and renderers of JSON data.
  • Direct transformations can be made between ABAP and JSON. Here, a canonical JSON representation of ABAP data called asJSON can be used for the identity transformation ID.

To learn more aout Json, Check one of the following JSON Books:

Handling of JSON in ABAP

The handling of JSON data in ABAP is based on the following principles:( source )

  • A JSON-XML format maps JSON data to XML.
  • XML readers and XML writers in sXML Library support JSON-XML and can be used as parsers and renderers of JSON data.
  • Direct transformations can be made between ABAP and JSON. Here, a canonical JSON representation of ABAP data called as JSON can be used for the identity transformation ID.

IF_SXML=>CO_XT_JSON Interface

JSON-XML is a special XML format that enables JSON data to be described using an XML representation. A new format, IF_SXML=>CO_XT_JSON, has been added to the sXML Library, which enables to handle JSON using JSON-XML

The canonical JSON representation asJSON defines a mapping between ABAP types and JSON.
This is used in serializations and deserializations using the identity transformation ID.

JSON data can be specified in various forms as an XML source in the statement CALL TRANSFORMATION and a JSON writer can be specified as target.
The identity transformation ID supports JSON by using asJSON.

Sample Implementation of sXML ABAP to JSON

Here an excellent sample for the implementation of IF_SXML=>CO_XT_JSON interface ( source )

First, you should create the ABAP to JSON Writer with the CL_SXML_STRING_WRITER with Json as Type.
Then Call the transformation to convert the String to JSON formatted String .

The last Step is to retrieve the Outpout of the Writer in JSON.

Convert JSON <> ABAP with /UI2/CL_JSON Class

The /UI2/CL_JSON Class is delivered with UI2 Add-on. A sample Implementatioh /UI2/CL_JSON is  (source)

This sample will include:

  • serialize table lt_flight into JSON, skipping initial fields and converting ABAP field names into camelCase
  • deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping
  • serialize ABAP object into JSON string

The most useful method for this API Json Class in ABAP are:

  • SERIALIZE: Serialize ABAP object into JSON
  • DESERIALIZE: Deserialize ABAP object from JSON string
  • GENERATE: Generates ABAP object from JSON
  • PRETTY_NAME : enumeration of modes, defined as constant /UI2/CL_JSON=>pretty_name.
  • ASSOC_ARRAYS :This option controls the way how hashed or sorted tables with unique keys serialized/deserialized

CL_TREX_JSON_SERIALIZER

An alternative to convert ABAP to JSON / SAP Internal Table to JSON is using the standard SAP class CL_TREX_JSON_SERIALIZER.

The CL_TREX_JSON SERIALIZER Class contains the following Methods:

  • CLASS_CONSTRUCTOR
  • CONSTRUCTOR
  • SERIALIZE
  • GET_DATA
  • RECURSE

You can read more this CL_TREX_JSON_SERIALIZER Class in this article ABAP to JSON / SAP Internal Table to JSON

ABAP JSON Parser And Mapper Sample Classes

In this section, you will find 2 opensources ABAP Class to handle all the actions when handling ABAP & JSON.

JSON Parser Class

A clean, reliable and compliant JSON parser and mapper to ABAP data the kind your mother would have encouraged you to hang out with.

JSON utility Class

Related SNOTES for Convert JSON & ABAP

  • 2292558 – Corrections for JSON serializer /UI2/CL_JSON
  • 2300508 – /UI2/CL_JSON deserialization of recursive structures
  • 2330592 – /UI2/CL_JSON corrections
  • 2368774 – /UI2/CL_JSON corrections – optimization for serialization of name/value tables
  • 2382783 – /UI2/CL_JSON corrections – bug fixes, supporting of firing of an exception on parsing error, helper methods for working with XSTRING output
  • 2429758 – /UI2/CL_JSON corrections – bug fixes control on serialization of NUMC types, support of on the fly ABAP data generation for unknown JSON structure
  • 2480119 – /UI2/CL_JSON corrections – fix for GENERATE method, fix for deserializing empty tables, auto-generation of initial fields with REF TO DATA, guided generation for initialized REF TO DATA fields (type of the referenced data is used)

More Resources ABAP & JSON Parser

I compiled here the most relevant links dealing with ABPA JSON Conversion and Parsing:

Related SAP Tutorials