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: 

JSON encoding

0 Kudos

Hello all

I need to send JSON data to a customer, for example :

[{"PERNR":"00000001","NACHN":"Smith","VORNA":"Jürgen"}]

This data will be sent and received via flat files, and also via OData in SEGW (SAP Gateway Builder). So I assume the name "Jürgen" for example would need to be encoded ..

Questions:

1) Do I need to encode this to UTF-8 ?

2) If so, how do I do this in ABAP ?

(If seen  cl_abap_conv_out_ce=>create( encoding = '4110' ) - but this results in an XSTRING ? )

Thanks in advance !

Jan

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos

Of course it's XSTRING, how could it be stored another way? If you're in a Unicode system, characters in String or C are always stored in UTF-16, cannot be anything else (more exactly UCS-2).

7 REPLIES 7

larshp
Active Contributor
0 Kudos

see

Sandra_Rossi
Active Contributor
0 Kudos

Of course it's XSTRING, how could it be stored another way? If you're in a Unicode system, characters in String or C are always stored in UTF-16, cannot be anything else (more exactly UCS-2).

0 Kudos

Thanks Sandra, I understand that the result is XSTRING but my challenge is to now represent this as JSON data in an OData response - which I thought could only be of type STRING - therefore ü would be "&#252" or something similar ?

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
which I thought could only be of type STRING


What makes you think so?


OData is itself based on XML and/or JSON. You create the files as strings on your platform, but you send them around as UTF-8.


In fact I don't think you have to care at all. "Jürgen" should be translated to the appropriate UTF-8 representation by the OData framework, your'e using.


Horst

0 Kudos

Excellent, thanks for the confirmation - much appreciated !

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Of course, you have to test it

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

cl_abap_conv_out_ce=>create( encoding = '4110' )


A more convenient class is CL_ABAP_CODEPAGE, e.g.:


DATA: str  TYPE string,

      xstr TYPE xstring.

xstr = cl_abap_codepage=>convert_to(

  source   = str

  codepage = `UTF-8` ).

                                                                                                                                                                                                                            You get a codepage representation of a character string in an xstring. Method convert_from does it vice versa.

If you transform ABAP data with call transformation you can decide whether the result is a string or an xstring, where xstring is the recommended type.          


As Sandra said, ABAP characters are stored internally in UCS-2. But the standard transfer format for XML and JSON is UTF-8. Therfore, it makes sense to convert the internal ABAP representation into UTF-8 in an xstring.


Horst