Skip to main content

JSON integration (UBC.json)

Imagine having a rather complex JSON schema definition you have to create and map your data out of SAP ABAP into it.

Examples

JSON schema:

{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}

JSON matching to schema:

{
"firstName": "John",
"lastName": "Doe",
"age": 21
}

The basic concept is to see the JSON schema as a kind of "type definition" and the JSON as the actual payload "data definition". This is a rather simple schema example which could be translated to an ABAP definition very easy:

types begin of s_schema.
types first_name type string.
types last_name type string.
types age type i.
types end of s_schema

Declaring an ABAP structure based on this type allows to map to static types using all the IDE-features like auto-completion, code-checking and so on. Producing a JSON out of the ABAP type then is quite easy: /UBC/CL_JSON=>GET_INSTANCE( )->SERIALIZE( )

Consider the following:

  • there are some JSON names which could not be translated to JSON and vice-versa. Consider using Name-Mappings.
  • the pretty mode (snake-case, camel-case, ...) for your target.
danger

Imagine having more complex scenarios like wrapped/referenced JSON schemas, version updates, etc. This could lead in a very high effort over and over again, and prone to errors!

One-Stop-Shop "Type Generator"

To speed up the development and eraising possible errors while copying type definitions (which can really be very complex according to the JSON schema specification), make use of our Type Generator!

To do so, start the transaction /UBC/SCHEMA_STRUCT_G. Schema generator input

  • Specify the Schema Id (URL)
  • Choose wether to
    • Upload each JSON schema by opening and downloading You will be prompted for every Schema not given yet. This especially is needed for system without internet connection
    • Automatically download JSON schema You will be prompted for RFC destinations pointint to the base URLs of the JSON schemas (because of the possibility for cross-domain referencing). So for example, if the JSON schema is located at https://json-schema.org/schema1.json you will be prompted for a RFC destination pointing to the base URL https://json-schema.org. If the schema1 is referring types from https://json-schema.org/schema2.json you will then not be prompted again.

As a result you will be able to copy the types definition to your class/interface making use of it. Schema generator result Optionally, if a name cannot be represented with a name-mapping (special characters in definition) there will be a name mapping table given to apply later to the JSON deserializiation.

Do your mapping to the ABAP type and then simply call /UBC/CL_JSON=>GET_INSTANCE( )->SERIALIZE( ). Make sure to provide the name-mapping for ABAP field names to JSON field names(if given by the generator).