Felix
Felix

Reputation: 88

What kind of data structure is needed to parse JSON to itab?

I want to parse a json string into an abap internal table, for example, this one

{
  "apiVersion": "1.0",
  "data": {
    "location": "Dresden",
    "temperature": "7",
    "skytext": "Light rain",
    "humidity": "96",
    "wind": "7.31 km/h",
    "date": "02-14-2017",
    "day": "Tuesday"
  }
}

I want to use the method cl_fdt_json=>json_to_data and put the values and keys into a table like this

      types: begin of map,
             key type string,
             value type string,
             end of map.
    data json_data type standard table of map.

But, unfortunately, it does not work like that. Does anyone have experience with this kind of problem? I don't have access to all the documentation because this is my sample task for a hirement to SAP and this is the last part of the "puzzle" ;) It is hard for me to find the solution.

Thank you!!!

EDIT: accordingly to vwegerts answer I tried the following. This is a little bit different to what i originally wanted to do, but it would also be ok)

  DATA cl_oops TYPE REF TO cx_dynamic_check.
  DATA(text) = result.
  TYPES: BEGIN OF ty_structure,
           skytext TYPE string,
           location type string,
           temperature type string,
           humidity type string,
           wind type string,
           date type string,
           day type string,
         END OF ty_structure.
  DATA : wa_structure TYPE ty_structure.
  TRY.
      CALL TRANSFORMATION id
           SOURCE XML text
           RESULT data = wa_structure.
      message  wa_structure-skytext type 'I'.
    CATCH cx_transformation_error INTO cl_oops.
      WRITE cl_oops->get_longtext( ).
  ENDTRY.

but it still doesnt work. when i check the value of wa_structure-skytext it is unfortunatly empty. i cannot find the mistake. does anyone have an idea?

Upvotes: 1

Views: 5701

Answers (4)

Suncatcher
Suncatcher

Reputation: 10621

You can do it like that via SAP JSON-XML reader:

CLASS lcl_json DEFINITION.
  PUBLIC SECTION.
    TYPES: BEGIN OF map,
             key   TYPE string,
             value TYPE string,
           END OF map,
           tt_map TYPE STANDARD TABLE OF map WITH DEFAULT KEY.

    CLASS-METHODS: parse IMPORTING iv_json       TYPE string
                         RETURNING VALUE(rv_map) TYPE tt_map.
ENDCLASS.

CLASS lcl_json IMPLEMENTATION.
  METHOD parse.
    DATA(o_reader) = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( iv_json ) ).
    TRY.
        DATA(o_node) = o_reader->read_next_node( ).
        WHILE o_node IS BOUND.
          CASE o_node->type.
            WHEN if_sxml_node=>co_nt_element_open.
              DATA(op) = CAST if_sxml_open_element( o_node ).
              LOOP AT op->get_attributes( ) ASSIGNING FIELD-SYMBOL(<a>).
                APPEND VALUE #( key = <a>->get_value( ) ) TO rv_map ASSIGNING FIELD-SYMBOL(<json>).
              ENDLOOP.
            WHEN if_sxml_node=>co_nt_value.
              DATA(val) = CAST if_sxml_value_node( o_node ).
              <json>-value = val->get_value( ).
            WHEN OTHERS.
          ENDCASE.
          o_node = o_reader->read_next_node( ).
        ENDWHILE.

      CATCH cx_root INTO DATA(e_txt).
        RAISE EXCEPTION TYPE cx_sxml_parse_error EXPORTING error_text = e_txt->get_text( ).
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

  DATA(json_string) = ` {"apiVersion":"1.0", ` &&
  ` "data":{ "location":"Dresden", "temperature":"7",` &&
  ` "skytext":"Light rain", "humidity":"96", "wind":"7.31 km/h", "date":"02-14-2017", "day":"Tuesday" } } `.
  TRY.
      DATA(it_map) = lcl_json=>parse( json_string ).
    CATCH cx_root INTO DATA(e_txt).
      " do handling
  ENDTRY.

Upvotes: 0

Eduardo Copat
Eduardo Copat

Reputation: 5151

Besides @vwegert recommendation to use the SAP documented json transformations, you could check the open source alternatives. This one looks promising.

Upvotes: 1

futu
futu

Reputation: 897

{"apiVersion":"1.0", "data":{ "location":"Dresden", "temperature":"7", "skytext":"Light rain", "humidity":"96", "wind":"7.31 km/h", "date":"02-14-2017", "day":"Tuesday" } }

The corresponding structure in ABAP would be:

"The nested data table 
Types: Begin of ty_data,
        location TYPE string,
        temperature TYPE string,
        skytext TYPE string,
        etc.
      End of ty_data,
      ty_t_data TYPE STANDARD TABLE OF ty_data WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE 0.
"the whole json structure
Types: Begin of ty_json,
        apiversion TYPE string,
        data TYPE ty_t_data,
       End of ty_json.
DATA: ls_data TYPE ty_json.

Now you have to find a proper JSON deserializer, which handles nested tables. Most deserializer expect a table input, so you have to add '['... ']' at the end of your JSON string and define lt_data TYPE STANDARD TABLE OF ty_json.

Upvotes: 0

vwegert
vwegert

Reputation: 18483

Rather than use the FDT class (which might not be available on all systems), you might want to take a look at the well-documented capabilities of the ABAP runtime system itself. This example program might be the way to go for you. You would essentially provide a Simple Transformation that would map the JSON XML structure to your data structure, instantiate a sXML JSON reader and then pass that as source to CALL TRANSFORMATION.

Upvotes: 1

Related Questions