Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I convert a JSON string to XML in ABAP

purayil_t
Explorer
0 Kudos

Hello All,

   I have a JSON string, which I get from my javascript. It is a complex JSON with nodes inside nodes.

I would like to create an XML from this JSON Object. And use this XML for further processing. I tried few things mentioned in SCN but did not work out properly. Could you please help me with this.

for ex. the JSON structure I get is

{

"is_model":

     {

          "t":"DD",

          "i":"Value for DD"

     },

"is_qty":

     {

          "t":"TX",

          "i":"2016

     },

"is_ctl":

     {

          "t":"DD",

          "i":"Single Apex"

     },

"is_ctr_qty":

     {

          "t":"TX",

          "i":"2200"

     }

}

Regards,

1 ACCEPTED SOLUTION

purayil_t
Explorer

Hello All,

   Thanks for your reply, I got it done with the sXML Library . IF_SXML_READER

Regards,

Purayil.

6 REPLIES 6

0 Kudos

Hello Venkat,

  What I did is

writer = cl_sxml_string_writer=>create(  ).
CALL TRANSFORMATION id SOURCE text = json_string
                        RESULT XML writer.
xml = cl_abap_codepage=>convert_from( writer->get_output( ) ).


But this did not help. am I missing something here.


Regards,

Purayil

Sandra_Rossi
Active Contributor

Hi. You need first a json reader. Using the ID transformation, I think it cannot go directly to an XML, it has to go to an ABAP variable (structure in your case). This structure will have the 4 components you have mentioned (IS_MODEL, IS_QTY...) Then do a second ID transformation from the variable to an XML string. Not sure whether it can be direct from JSON to XML.

purayil_t
Explorer

Hello All,

   Thanks for your reply, I got it done with the sXML Library . IF_SXML_READER

Regards,

Purayil.

0 Kudos

hi, how did you find a solution? Can you share an example?

0 Kudos

Hello Cadir,

Here is the code I have. It has got lot of data declerations which is not be needed, but since I trimmed down only conversion you can still use it.

DATA: lv_json TYPE string,

lr_reader TYPE REF TO if_sxml_reader,

lr_writer TYPE REF TO cl_sxml_string_writer,

lv_xxml TYPE xstring,

lv_xml TYPE string,

lv_html TYPE string,

lr_sxml_node TYPE REF TO if_sxml_node.

" Data Declaration for Sending Email

DATA: lv_successTYPE os_boolean,

lv_email_id TYPE adr6-smtp_addr,

lt_text TYPE bcsy_text,

lr_send_request TYPE REF TO cl_bcs,

lv_offset TYPE i,

lv_length TYPE i,

lv_html_lengthTYPE i,

lv_diff TYPE i,

lr_recipientTYPE REF TO if_recipient_bcs,

lr_sender TYPE REF TO cl_sapuser_bcs,

lr_document TYPE REF TO cl_document_bcs,

lv_solixTYPE soli.

" data Decleration for iterating through xml.

DATA: lr_iterator TYPE REF TO if_ixml_node_iterator,

lv_node_valTYPE string,

lv_node_namTYPE string,

lv_node_typ TYPE i,

lv_child_node_valTYPE string,

lv_child_node_namTYPE string,

lv_child_node_typ TYPE i,

lv_nodeattr_valTYPE string,

lv_nodeattr_namTYPE string,

lv_nodeattr_typ TYPE i,

ixml TYPE REF TO if_ixml,

stream_factory TYPE REF TO if_ixml_stream_factory,

document TYPE REF TO if_ixml_document,

lr_ixml_node TYPE REF TO if_ixml_node,

lr_ixml_texts_collection_node TYPE REF TO if_ixml_node,

lr_ixml_text_node TYPE REF TO if_ixml_node,

lr_node_attr TYPE REF TO if_ixml_node,

lr_attributes TYPE REF TO if_ixml_named_node_map,

lr_parse_error TYPE REF TO cx_sxml_parse_error,

lr_child_attributes TYPE REF TO if_ixml_named_node_map.

DATA: lr_node_child_list TYPE REF TO if_ixml_node_list,

lv_index TYPE i,

lv_child_length TYPE i,

lv_to_translate TYPE bool VALUE abap_false,

lr_node_to_translate TYPE REF TO if_ixml_node,

lr_child_node TYPE REF TO if_ixml_node.

"BREAK-POINT.

CONCATENATE

'{"0":{"t":"TB","i":[{"t":"TX","i":"120"},{"t":"TX","i":78}],"z":"X","k":"backteria_count","o":{"c":[{"n":"goal","t":"TX"},'

'{"n":"current","t":"TX"}]}},"1":{"t":"TB","i":[{"t":"TX","i":"124"},{"t":"TX","i":80}],"z":"X","k":"lpc","o":{"c":[{"n":"goal","t":"TX"},'

'{"n":"current","t":"TX"}]}},"2":{"t":"TB","i":[{"t":"TX","i":"125"},{"t":"TX","i":81}],"z":"X","k":"pi","o":{"c":[{"n":"goal","t":"TX"},'

'{"n":"current","t":"TX"}]}},"3":{"t":"TB","i":[{"t":"TX","i":"126"},{"t":"TX","i":81}],"z":"X","k":"cc","o":{"c":[{"n":"goal","t":"TX"},'

'{"n":"current","t":"TX"}]}},"4":{"t":"TB","i":[{"t":"TX","i":"127"},{"t":"TX","i":82}],"z":"X","k":"btscc","o":{"c":'

'[{"n":"goal","t":"TX"},{"n":"current","t":"TX"}]}}}'

INTO lv_json.

lr_reader = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( lv_json ) ).

lr_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_xml10 ).

" Convert JSON string to XSTRING

TRY.

lr_reader->next_node( ).

lr_reader->skip_node( lr_writer ).

lv_xxml =lr_writer->get_output( ) .

CATCH cx_sxml_parse_error INTO lr_parse_error.

ENDTRY.

**********************************************************************

" Parse JSON to XML. Outupt is in XSTRING

ixml = cl_ixml=>create( ).

stream_factory = ixml->create_stream_factory( ).

document = ixml->create_document( ).

IF ixml->create_parser(

document = document

stream_factory = stream_factory

istream = stream_factory->create_istream_xstring( string = lv_xxml )

)->parse( ) <> 0.

RETURN.

ENDIF.

"Create iXML document to iterate and modify elements

**********************************************************************

"Convert the XSTRING XML data to STRING and format it

lr_reader = cl_sxml_string_reader=>create( lv_xxml ).

lr_writer =cl_sxml_string_writer=>create( ) .

lr_writer->if_sxml_writer~set_option( option = if_sxml_writer=>co_opt_linebreaks ).

lr_writer->if_sxml_writer~set_option( option = if_sxml_writer=>co_opt_indent ).

lr_reader->next_node( ).

lr_reader->skip_node( lr_writer ).

" lr_reader->read_next_node( ).

lv_xml = cl_abap_codepage=>convert_from( lr_writer->get_output( ) ).

lv_xml = escape( val = lv_xml format = cl_abap_format=>e_xml_text ).

REPLACE ALL OCCURRENCES OF 'amplt' IN lv_xml WITH '<'.

CONCATENATE '<?xml version="1.0" encoding="UTF-8"?>' lv_xml INTO lv_xml.

BREAK-POINT.