ABAP Keyword Documentation → ABAP - Reference → Data Interfaces and Communication Interfaces → ABAP and JSON → JSON, Examples
JSON, Transforming Names
Transforms the names of JSON objects to uppercase letters.
Other versions:
7.31 | 7.40 | 7.54
Source Code
REPORT demo_json_names_to_upper.
CLASS json_demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
PRIVATE SECTION.
CLASS-METHODS:
json_names_to_upper_pr
IMPORTING in TYPE xstring
RETURNING VALUE(out) TYPE xstring,
json_names_to_upper_tr
IMPORTING in TYPE xstring
RETURNING VALUE(out) TYPE xstring.
ENDCLASS.
CLASS json_demo IMPLEMENTATION.
METHOD main.
DATA(out) = cl_demo_output=>new( ).
DATA: BEGIN OF struc,
col1 TYPE i,
col2 TYPE i,
END OF struc.
DATA(json) = cl_abap_codepage=>convert_to(
`{"struc":{"col1":1,"col2":2}}` ).
out->begin_section( 'Original JSON'
)->write_json( json ).
CALL TRANSFORMATION id SOURCE XML json RESULT struc = struc.
out->next_section( 'Deserialized JSON'
)->write( struc ).
DATA(asjson) = json_names_to_upper_pr( json ).
ASSERT asjson = json_names_to_upper_tr( json ).
out->begin_section( 'Modified JSON'
)->write_json( asjson ).
CALL TRANSFORMATION id SOURCE XML asjson RESULT struc = struc.
out->next_section( 'Deserialized JSON'
)->write( struc ).
out->display( ).
ENDMETHOD.
METHOD json_names_to_upper_pr.
DATA(reader) = cl_sxml_string_reader=>create( in ).
DATA(writer) = CAST if_sxml_writer(
cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ) ).
DO.
DATA(node) = reader->read_next_node( ).
IF node IS INITIAL.
EXIT.
ENDIF.
IF node->type = if_sxml_node=>co_nt_element_open.
DATA(attributes) = CAST if_sxml_open_element(
node )->get_attributes( ).
LOOP AT attributes ASSIGNING FIELD-SYMBOL(<attribute>).
IF <attribute>->qname-name = 'name'.
<attribute>->set_value(
to_upper( <attribute>->get_value( ) ) ).
ENDIF.
ENDLOOP.
ENDIF.
writer->write_node( node ).
ENDDO.
out = CAST cl_sxml_string_writer( writer )->get_output( ) .
ENDMETHOD.
METHOD json_names_to_upper_tr.
DATA(writer) =
cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION demo_json_xml_to_upper
SOURCE XML in
RESULT XML writer.
out = writer->get_output( ) .
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
json_demo=>main( ).
Description
This example demonstrates how the names of JSON data objects
can be transformed to uppercase letters, so making it possible, for example, to bind the objects to
the corresponding ABAP data in deserializations using the statement
CALL TRANSFORMATION
. Two transformation methods are demonstrated:
- Parsing and rendering in the method
json_names_to_upper_pr
- This transforms the JSON data to JSON-XML.
- The result is parsed using an XML reader.
- The attributes with the name name are transformed to uppercase letters as in the example Modifying XML Data.
- The result is rendered back to JSON using a JSON writer.
- Calling a transformation in the method
json_names_to_upper_tr
- The XSL transformation DEMO_JSON_XML_TO_UPPER, designed for this purpose, is used to transform the object names in JSON-XML to uppercase letters and passes the result to a JSON writer.
- The modified JSON data is extracted from the writer.
The statement ASSERT
guarantees that the results of both transformations
are the same. After the transformation, the data is deserialized successfully to the ABAP structure. The method used in practice depends on performance and the volume of data expected.
The XSL transformation used, DEMO_JSON_XML_TO_UPPER, is as follows:
xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="name">
<xsl:value-of select="sap:upper-case(.)"/>
</xsl:attribute>
</xsl:template>
</xsl:transform>