Application Development and Automation 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: 
Read only

Serialize json to ABAP

naotoxxx
Participant
0 Likes
2,967

Hello i'm triying to use /UI2/CL_JSON class => SERIALIZE() i pass my json and create my structure but my structured is not filled.

TYPES: BEGIN OF ty_a,
        BUKRS(25),
        VSTEL(25),
        VKORG(25),
        FECHA(25),
        CANAL(25),
        SECTOR(25),
        SOLIC(25),
       END OF ty_a,
       BEGIN OF ty_b,
        pedido(25),
        werks(25),
        posiciones TYPE zborrar_posiciones,"TYPE TABLE of a structure with two fields: matnr and ctdad
        suppl_plnt(25),
        lotno(25),
        bokno(25),
        kschl(25),
        trans(25),
        printer(25),
        msg(25),
       END OF ty_b,
       BEGIN OF ty_x,
        accion(10),
        input TYPE ty_a,
        data_cont TYPE ty_b,
       END OF ty_x.


DATA: ls_info TYPE ty_x.
* deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping
DATA: str1 TYPE string VALUE '"accion":"CREA_LA","input":{"BUKRS":"","VSTEL":"","VKORG":"","FECHA":"","CANAL":"","SECTOR":"","SOLIC":""},"data_cont":{"pedido":"219135968","werks":"1100"'.
DATA: str2 TYPE string VALUE ',"suppl_plnt":"","lotno":"","bokno":"","kschl":"","trans":"","printer":"","msg":""}'.
DATA: final TYPE string.

CONCATENATE str1 str2 INTO final.
/ui2/cl_json=>deserialize( EXPORTING json = final pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_info ).

The last line it's executed but is not filling my structure, what am i doing wrong ???

1 ACCEPTED SOLUTION
Read only

pfefferf
Active Contributor
1,832

naotoxxx As your JSON string is not a valid JSON the deserialization does nothing. Why your string is not a valid JSON string? Because the opening and closing curly brackets are missing. In the following formatted example of your string, the first and last line I added.

{ 
	"accion": "CREA_LA",
	"input": {
		"BUKRS": "",
		"VSTEL": "",
		"VKORG": "",
		"FECHA": "",
		"CANAL": "",
		"SECTOR": "",
		"SOLIC": ""
	},
	"data_cont": {
		"pedido": "219135968",
		"werks": "1100",
		"suppl_plnt": "",
		"lotno": "",
		"bokno": "",
		"kschl": "",
		"trans": "",
		"printer": "",
		"msg": ""
	}
}

Hello i'm triying to use /UI2/CL_JSON class => SERIALIZE() i pass my json and create my structure but my structured is not filled.

TYPES: BEGIN OF ty_a,
        BUKRS(25),
        VSTEL(25),
        VKORG(25),
        FECHA(25),
        CANAL(25),
        SECTOR(25),
        SOLIC(25),
       END OF ty_a,
       BEGIN OF ty_b,
        pedido(25),
        werks(25),
        posiciones TYPE zborrar_posiciones,"TYPE TABLE of a structure with two fields: matnr and ctdad
        suppl_plnt(25),
        lotno(25),
        bokno(25),
        kschl(25),
        trans(25),
        printer(25),
        msg(25),
       END OF ty_b,
       BEGIN OF ty_x,
        accion(10),
        input TYPE ty_a,
        data_cont TYPE ty_b,
       END OF ty_x.


DATA: ls_info TYPE ty_x.
* deserialize JSON string json into internal table lt_flight doing camelCase to ABAP like field name mapping
DATA: str1 TYPE string VALUE '"accion":"CREA_LA","input":{"BUKRS":"","VSTEL":"","VKORG":"","FECHA":"","CANAL":"","SECTOR":"","SOLIC":""},"data_cont":{"pedido":"219135968","werks":"1100"'.
DATA: str2 TYPE string VALUE ',"suppl_plnt":"","lotno":"","bokno":"","kschl":"","trans":"","printer":"","msg":""}'.
DATA: final TYPE string.

CONCATENATE str1 str2 INTO final.
/ui2/cl_json=>deserialize( EXPORTING json = final pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_info ).

The last line it's executed but is not filling my structure, what am i doing wrong ???

2 REPLIES 2
Read only

pfefferf
Active Contributor
1,833

naotoxxx As your JSON string is not a valid JSON the deserialization does nothing. Why your string is not a valid JSON string? Because the opening and closing curly brackets are missing. In the following formatted example of your string, the first and last line I added.

{ 
	"accion": "CREA_LA",
	"input": {
		"BUKRS": "",
		"VSTEL": "",
		"VKORG": "",
		"FECHA": "",
		"CANAL": "",
		"SECTOR": "",
		"SOLIC": ""
	},
	"data_cont": {
		"pedido": "219135968",
		"werks": "1100",
		"suppl_plnt": "",
		"lotno": "",
		"bokno": "",
		"kschl": "",
		"trans": "",
		"printer": "",
		"msg": ""
	}
}
Read only

0 Likes
1,832

Oh man thanks a lot !. From now i'm going to check the format always!