cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing JSON data

Stephen3
Participant
0 Kudos

Hi

I am using the code below to parse a JSON input string of data. This is from SAP Help and is working. My input JSON string is as follows:

{"SOURCE":"AUTONIQ",'.
"TARGET":"SALESTAX",'.
'"VERSION":"1.0",'.
'"FORMAT":"JSON",'.
'"DATA":{"COMPANY":"1000",'.
'"TAX_CODE":"O1",'.
'"JURISDICTION":"NY005441011",'.
'"CURRENCY":"USD",'.
'"AMOUNT":100.00'.
}

}

We are trying to develop a generic json service that can be called by the outside world. Our internal "dispatcher" will read the value assigned to parameter "TARGET" and then feed all the data after the "DATA" parameter to the appropriate function module. Can anyone assist me in enhancing the code below to find the value assigned to parameter "TARGET" and to create a string variable with all the data after the key word "DATA" (i.e. all the JSON data after the first { bracket to the closing } bracket.

Thanks

Stephen

TRY.
 DO.
 CLEAR node_wa.
 data(node) = reader->read_next_node( ).
 IF node is INITIAL.
 EXIT.
 ENDIF.
 writer->write_node( node ).
 CASE node->type.
 WHEN if_sxml_node=>co_nt_element_open.
 DATA(open_element) = CAST if_sxml_open_element( node ).
 node_wa-node_type = `open element`.
 node_wa-prefix = open_element->prefix.
 node_wa-name = open_element->qname-name.
 node_wa-nsuri = open_element->qname-namespace.
 DATA(attributes) = open_element->get_attributes( ).
 APPEND node_wa TO nodes.
 LOOP AT attributes INTO DATA(attribute).
 node_wa-node_type = `attribute`.
 node_wa-prefix = attribute->prefix.
 node_wa-name = attribute->qname-name.
 node_wa-nsuri = attribute->qname-namespace.
 IF attribute->value_type = if_sxml_value=>co_vt_text.
 node_wa-value = attribute->get_value( ).
 ELSEIF attribute->value_type = if_sxml_value=>co_vt_raw.
 node_wa-value_raw = attribute->get_value_raw( ).
 ENDIF.
 APPEND node_wa TO nodes.
 ENDLOOP.
 CONTINUE.
 WHEN if_sxml_node=>co_nt_element_close.
 DATA(close_element) = CAST if_sxml_close_element( node ).
 node_wa-node_type = `close element`.
 node_wa-prefix = close_element->prefix.
 node_wa-name = close_element->qname-name.
 node_wa-nsuri = close_element->qname-namespace.
 APPEND node_wa TO nodes.
 CONTINUE.
 WHEN if_sxml_node=>co_nt_value.
 DATA(value_node) = CAST if_sxml_value_node( node ).
 node_wa-node_type = `value`.
 IF value_node->value_type = if_sxml_value=>co_vt_text.
 node_wa-value = value_node->get_value( ).
 ELSEIF value_node->value_type = if_sxml_value=>co_vt_raw.
 node_wa-value_raw = value_node->get_value_raw( ).
 ENDIF.
 APPEND node_wa TO nodes.
 CONTINUE.
 WHEN OTHERS.
 node_wa-node_type = `Error`.
 APPEND node_wa TO nodes.
 EXIT.
 ENDCASE.
 ENDDO.
 CATCH cx_sxml_parse_error INTO DATA(parse_error).
 out->write_text( parse_error->get_text( ) ).
 ENDTRY.

Accepted Solutions (0)

Answers (0)