SAP ABAP JSON
From SapWiki
Contents
Ejemplo XSLT JSON
Uso en programas ABAP (XSLT)
FORM crear_json USING p_guia TYPE zhr01_dgtl p_uname CHANGING p_string_json TYPE string. DATA: json_writer TYPE REF TO cl_sxml_string_writer. DATA json TYPE xstring. DATA lt_guia TYPE TABLE OF zhr01_dgtl. APPEND p_guia TO lt_guia. json_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). CALL TRANSFORMATION zhcm_guia_dgtl SOURCE root = lt_guia usuario = p_uname RESULT XML json_writer. "se usa el ejemplo 2 de Ejemplos json = json_writer->get_output( ). * PERFORM encoding2 CHANGING json p_string_json. PERFORM decode USING json CHANGING p_string_json. ENDFORM. " CREAR_JSON FORM decode USING p_xstring CHANGING p_string. CALL METHOD cl_http_utility=>if_http_utility~decode_utf8 EXPORTING encoded = p_xstring RECEIVING unencoded = p_string. ENDFORM. "decode
Ejemplo Serializing JSON
*&---------------------------------------------------------------------* *& Report YTEST_JSON3 *&---------------------------------------------------------------------* *& ejemplo Serializing JSON *&---------------------------------------------------------------------* REPORT ytest_json3 LINE-SIZE 260. DATA l_string TYPE string. TYPES: BEGIN OF ty_t001, bukrs TYPE t001-bukrs, butxt TYPE t001-butxt, ort01 TYPE t001-ort01, land1 TYPE t001-land1, waers TYPE t001-waers, spras TYPE t001-spras, END OF ty_t001. DATA lt_data TYPE TABLE OF ty_t001. SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_data FROM t001 UP TO 2 ROWS where land1 = 'DE'. CALL METHOD /ui2/cl_json=>serialize EXPORTING data = lt_data " Data to serialize RECEIVING r_json = l_string. " JSON string WRITE:/ l_string. CALL METHOD /ui2/cl_json=>serialize EXPORTING data = lt_data " Data to serialize pretty_name = /ui2/cl_json=>pretty_mode-low_case "camel_case RECEIVING r_json = l_string. " JSON string WRITE:/ l_string. CALL METHOD /ui2/cl_json=>serialize EXPORTING data = lt_data " Data to serialize pretty_name = /ui2/cl_json=>pretty_mode-low_case "camel_case name = 'SOCIEDADES' RECEIVING r_json = l_string. " JSON string WRITE:/ l_string. TYPES: BEGIN OF ty_data, nombre TYPE string, edad TYPE i, nacimiento TYPE d, direccion TYPE string, plata(10) TYPE p DECIMALS 0, END OF ty_data. DATA wa TYPE ty_data. wa-nombre = 'Deadpool'. wa-edad = 1000. wa-nacimiento = '19720915'. wa-direccion = 'USA'. wa-plata = 999999. CALL METHOD /ui2/cl_json=>serialize EXPORTING data = wa " Data to serialize RECEIVING r_json = l_string. " JSON string WRITE:/ l_string. *#################################################################### TYPES: BEGIN OF tp_s_data, sschema TYPE string, odatacontext TYPE string, shortened_abap_name TYPE string, standard TYPE string, END OF tp_s_data. DATA: ls_exp TYPE tp_s_data, lt_mapping TYPE /ui2/cl_json=>name_mappings, lv_json TYPE /ui2/cl_json=>json. lt_mapping = VALUE #( ( abap = `SSCHEMA` json = `$schema` ) ( abap = `ODATACONTEXT` json = `@odata.context` ) ( abap = `SHORTENED_ABAP_NAME` json = `VeeeeryyyyyLooooongJSONAttrbuuuuuuuuuteeeeeeeeeee` ) ). ls_exp-sschema = 'sss'. ls_exp-odatacontext = 'odata'. ls_exp-shortened_abap_name = 'ssssname'. ls_exp-standard = 'stan'. lv_json = /ui2/cl_json=>serialize( data = ls_exp name_mappings = lt_mapping ). WRITE:/ lv_json.
- Salida Reporte
[{"BUKRS":"0001","BUTXT":"SAP A.G.","ORT01":"Walldorf","LAND1":"DE","WAERS":"EUR","SPRAS":"D"},{"BUKRS":"COPY","BUTXT":"Copy from CC.0001","ORT01":"(Only G/L accounts B-seg)","LAND1":"DE","WAERS":"EUR","SPRAS":"D"}] [{"bukrs":"0001","butxt":"SAP A.G.","ort01":"Walldorf","land1":"DE","waers":"EUR","spras":"D"},{"bukrs":"COPY","butxt":"Copy from CC.0001","ort01":"(Only G/L accounts B-seg)","land1":"DE","waers":"EUR","spras":"D"}] "SOCIEDADES":[{"bukrs":"0001","butxt":"SAP A.G.","ort01":"Walldorf","land1":"DE","waers":"EUR","spras":"D"},{"bukrs":"COPY","butxt":"Copy from CC.0001","ort01":"(Only G/L accounts B-seg)","land1":"DE","waers":"EUR","spras":"D"}] {"NOMBRE":"Deadpool","EDAD":1000,"NACIMIENTO":"1972-09-15","DIRECCION":"USA","PLATA":999999} {"$schema":"sss","@odata.context":"odata","VeeeeryyyyyLooooongJSONAttrbuuuuuuuuuteeeeeeeeeee":"ssssname","STANDARD":"stan"}
Ejemplo de programa que genera archivo XSLT JSON, útil para Json con muchos campos
*&---------------------------------------------------------------------* *& Report YJSON *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT yjson. TYPES: BEGIN OF ty_fields, fieldname TYPE string, END OF ty_fields. DATA lt_fields TYPE TABLE OF ty_fields. DATA ls_fields TYPE ty_fields. DATA l_string TYPE string. DEFINE m_append. CLEAR ls_fields. ls_fields-fieldname = &1. APPEND ls_fields TO lt_fields. END-OF-DEFINITION. START-OF-SELECTION. m_append 'nombre'. m_append 'apellido'. m_append 'uvpAsignacionTraslacion'. m_append 'mesUvpAsignacionTraslacion'. m_append 'anoUvpAsignacionTraslacion'. m_append 'uvpAsignMovilizacionEjec'. m_append 'mesUvpAsignMovilizacionEjec'. m_append 'anoUvpAsignMovilizacionEjec'. m_append 'sueldoBaseMensual'. m_append 'mesSueldoBaseMensual'. m_append 'anoSueldoBaseMensual'. m_append 'posicion'. m_append 'proporcionalGratificacion'. m_append 'mesProporcionalGratificacion'. m_append 'anoProporcionalGratificacion'. m_append 'anoPagoGratificacion'. m_append 'proporcionalAnticipoGratificacion'. m_append 'mesProporcionalAnticipoGratificacion'. m_append 'anoProporcionalAnticipoGratificacion'. m_append 'anoPagoAnticipoGratificacion'. m_append 'proporcionalProvBonoBagEjec'. m_append 'mesProporcionalProvBonoBagEjec'. m_append 'anoProporcionalProvBonoBagEjec'. m_append 'anoPagoProvBonoBagEjec'. m_append 'uvpAsigTransporte'. m_append 'mesUvpAsigTransporte'. m_append 'anoUvpAsigTransporte'. m_append 'uvpAsigAlimentacion'. m_append 'mesUvpAsigAlimentacion'. m_append 'anoUvpAsigAlimentacion'. m_append 'saldoPendienteVacacionesLegales'. m_append 'saldoPendienteVacacionesContract'. m_append 'saldoPendienteVacacionesProgresivas'. m_append 'uvpAporteGestionDesempeno'. m_append 'mesUvpAporteGestionDesempeno'. m_append 'anoUvpAporteGestionDesempeno'. m_append 'antiguedad'. m_append 'uvpBecaExcelenciaTrabajado'. m_append 'mesUvpBecaExcelenciaTrabajado'. m_append 'anoUvpBecaExcelenciaTrabajado'. m_append 'uvpBecaEscPreEscolar'. m_append 'mesUvpBecaEscPreEscolar'. m_append 'anoUvpBecaEscPreEscolar'. m_append 'uvpBecaBasicaMedia'. m_append 'mesUvpBecaBasicaMedia'. m_append 'anoUvpBecaBasicaMedia'. m_append 'uvpBecaSuperior'. m_append 'mesUvpBecaSuperior'. m_append 'anoUvpBecaSuperior'. m_append 'uvpBecaExcelenciaBenefic'. m_append 'mesUvpBecaExcelenciaBenefic'. m_append 'anoUvpBecaExcelenciaBenefic'. m_append 'totalSubsidiadoFecha'. m_append 'mesTotalSubsidiadoFecha'. m_append 'anoTotalSubsidiadoFecha'. m_append 'totalSubsidiadoFechaDSB'. m_append 'mesTotalSubsidiadoFechaDSB'. m_append 'anoTotalSubsidiadoFechaDSB'. m_append 'asignacionFaenaCordillera'. m_append 'mesAsignacionFaenaCordillera'. m_append 'anoAsignacionFaenaCordillera'. m_append 'proporcionalBonoVacacionesSPS'. m_append 'mesProporcionalBonoVacacionesSPS'. m_append 'anoProporcionalBonoVacacionesSPS'. m_append 'proporcionalAnticipoBonoVacacionesSPS'. m_append 'mesProporcionalAnticipoBonoVacacionesSPS'. m_append 'anoProporcionalAnticipoBonoVacacionesSPS'. m_append 'uvpAsignacionBonoVacaSPS'. m_append 'mesUvpAsignacionBonoVacaSPS'. m_append 'anoUvpAsignacionBonoVacaSPS'. m_append 'uvpAguinaldoFiestasPatrias'. m_append 'mesUvpAguinaldoFiestasPatrias'. m_append 'anoUvpAguinaldoFiestasPatrias'. m_append 'uvpAguinaldoNavidad'. m_append 'mesUvpAguinaldoNavidad'. m_append 'anoUvpAguinaldoNavidad'. m_append 'proporcionalAnticipoBonoFlexible'. m_append 'mesProporcionalAnticipoBonoFlexible'. m_append 'anoProporcionalAnticipoBonoFlexible'. m_append 'anoPagoAnticipoBonoFlexible'. m_append 'uvpAsignMovilizacion'. m_append 'mesUvpAsignMovilizacion'. m_append 'anoUvpAsignMovilizacion'. m_append 'uvpAsignAlimentacion'. m_append 'mesUvpAsignAlimentacion'. m_append 'anoUvpAsignAlimentacion'. m_append 'uvpAsignColacion'. m_append 'mesUvpAsignColacion'. m_append 'anoUvpAsignColacion'. m_append 'uvpAsignacionTurnoNoche'. m_append 'mesUvpAsignacionTurnoNoche'. m_append 'anoUvpAsignacionTurnoNoche'. m_append 'anoPagoAporteGestionDesempeno'. m_append 'uvpIncTrimGestionNegocio'. m_append 'mesUvpIncTrimGestionNegocio'. m_append 'anoUvpIncTrimGestionNegocio'. m_append 'uvpIncentivosGestionMensual'. m_append 'mesUvpIncentivosGestionMensual'. m_append 'anoUvpIncentivosGestionMensual'. m_append 'uvpAsignRiesgoCambioTurno'. m_append 'mesUvpAsignRiesgoCambioTurno'. m_append 'anoUvpAsignRiesgoCambioTurno'. m_append 'uvpAsigTempRespEjec'. m_append 'mesUvpAsigTempRespEjec'. m_append 'anoUvpAsigTempRespEjec'. m_append 'uvpAsignacionAcademicaSPS'. m_append 'mesUvpAsignacionAcademicaSPS'. m_append 'anoUvpAsignacionAcademicaSPS'. m_append 'uvpBonoCompBenSalaCuna'. m_append 'mesUvpBonoCompBenSalaCuna'. m_append 'anoUvpBonoCompBenSalaCuna'. m_append 'uvpAsignacionUtilesSPS'. m_append 'mesUvpAsignacionUtilesSPS'. m_append 'anoUvpAsignacionUtilesSPS'. m_append 'uvpAsignacionCapacitacion'. m_append 'mesUvpAsignacionCapacitacion'. m_append 'anoUvpAsignacionCapacitacion'. m_append 'uvpAsignacionDiasFestivos'. m_append 'mesUvpAsignacionDiasFestivos'. m_append 'anoUvpAsignacionDiasFestivos'. m_append 'permisoAdministratSPS'. m_append 'uvpAsignacionFallecimiento'. m_append 'mesUvpAsignacionFallecimiento'. m_append 'anoUvpAsignacionFallecimiento'. m_append 'asignacionMaternidad'. m_append 'mesAsignacionMaternidad'. m_append 'anoAsignacionMaternidad'. m_append 'uvpDepConvenidoMensual'. m_append 'mesUvpDepConvenidoMensual'. m_append 'anoUvpDepConvenidoMensual'. m_append 'uvpAsignControlMedicoOcup'. m_append 'mesUvpAsignControlMedicoOcup'. m_append 'anoUvpAsignControlMedicoOcup'. m_append 'uvpBecaConyuge'. m_append 'mesUvpBecaConyuge'. m_append 'anoUvpBecaConyuge'. data(l_e) = CL_ABAP_CHAR_UTILITIES=>CR_LF. LOOP AT lt_fields INTO ls_fields. l_string = |<object>{ l_e }<str name="{ ls_fields-fieldname }">{ l_e }<tt:value ref="$ref.{ ls_fields-fieldname }"/>{ l_e }</str>{ l_e }</object>{ l_e }|. IF sy-tabix = 1. DATA(l_string_json) = l_string. ELSE. CONCATENATE l_string_json l_string INTO l_string_json. ENDIF. ENDLOOP. BREAK-POINT.
- salida
<object> <str name="nombre"> <tt:value ref="$ref.nombre"/> </str> </object> <nowiki><object> <str name="apellido"> <tt:value ref="$ref.apellido"/> </str> </object> <object> <str name="uvpAsignacionTraslacion"> <tt:value ref="$ref.uvpAsignacionTraslacion"/> </str> </object> <object> <str name="mesUvpAsignacionTraslacion"> <tt:value ref="$ref.mesUvpAsignacionTraslacion"/> </str> </object> ... ... ... <object> <str name="anoUvpBecaConyuge"> <tt:value ref="$ref.anoUvpBecaConyuge"/> </str> </object>
Caracteres especiales
" Escape special characters REPLACE ALL OCCURRENCES OF '\' IN c_string WITH '\\'. REPLACE ALL OCCURRENCES OF '/' IN c_string WITH '\/'. REPLACE ALL OCCURRENCES OF '"' IN c_string WITH '\"'. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf(1) IN c_string WITH '\r'. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>newline IN c_string WITH '\n'. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN c_string WITH '\t'. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>form_feed IN c_string WITH '\f'. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>vertical_tab IN c_string WITH '\v'. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>backspace IN c_string WITH '\b'. " replace escape characters REPLACE ALL OCCURRENCES OF '\n' IN c_string WITH cl_abap_char_utilities=>newline. REPLACE ALL OCCURRENCES OF '\t' IN c_string WITH cl_abap_char_utilities=>horizontal_tab. REPLACE ALL OCCURRENCES OF '\f' IN c_string WITH cl_abap_char_utilities=>form_feed. REPLACE ALL OCCURRENCES OF '\v' IN c_string WITH cl_abap_char_utilities=>vertical_tab. REPLACE ALL OCCURRENCES OF '\b' IN c_string WITH cl_abap_char_utilities=>backspace. REPLACE ALL OCCURRENCES OF '\r' IN c_string WITH cl_abap_char_utilities=>cr_lf(1). REPLACE ALL OCCURRENCES OF '\"' IN c_string WITH '"'. REPLACE ALL OCCURRENCES OF '\/' IN c_string WITH '/'. REPLACE ALL OCCURRENCES OF '\\' IN c_string WITH '\'.