Skip to Content
Personal Insights
Author's profile photo Łukasz Pęgiel

JSON2ABAPType – thank you for contribution

Some time ago I’ve posted on github a code of the program which helps to create an ABAP type on a base of JSON structure. Such program is very useful when you work with web-services or web-hooks, as you can automate the creation of the types for your coding. The program you can install using abapGIT and git repository found at https://github.com/fidley/JSON2ABAPType

 

But I do not write this post to let you know about this program, I write it because lately https://people.sap.com/marspark added some additional features to it, like keeping the original sequence of the fields, or keeping the parameters for the conversion always on the screen, making it more user friendly.

 

 

 

Such contributions to any of the code I’ve published so far, makes me feel really good and it’s even more enjoyable to see others contributing to your work, than publishing it yourself.

 

Thank you for your contribution https://people.sap.com/marspark and keep up good work!

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michael Keller
      Michael Keller

      Thumbs up for his work! This is one of the brief moments to see the future of the SAP ecosystem: crowdsourcing, open source and other things becoming more important. No one can master every technology, so we all need to work more together.

      Author's profile photo Łukasz Pęgiel
      Łukasz Pęgiel
      Blog Post Author

      I cannot agree more!

      Author's profile photo Zhe Zhao
      Zhe Zhao

      Thank you, I am very honored. I also agree with the comment of Michael. An open ecosystem will be better, because it makes everyone easier to contribute their ideas and work.

      Author's profile photo Łukasz Pęgiel
      Łukasz Pęgiel
      Blog Post Author

      You've deserved it!

      Author's profile photo Haorong Liang
      Haorong Liang

      Thanks for sharing, btw, I found a bug and try to impove it as below, the method should be consider the node level and sorting 🙂

       

      METHOD create_types.
      DATA: components TYPE string.
      LOOP AT hierarchy ASSIGNING FIELD-SYMBOL(<h>).
      IF <h>-structure EQ abap_true AND <h>-type NE 'g'.
      <h>-final_type = |{ <h>-name } type t_{ <h>-name }{ <h>-id }|.
      <h>-type_definition = |types: begin of t_{ <h>-name }{ <h>-id },{ cl_abap_char_utilities=>newline }{ c_components }end of t_{ <h>-name }{ <h>-id }.|.
      ELSEIF <h>-structure EQ abap_true.
      <h>-final_type = |{ <h>-name } type t_{ <h>-name }{ <h>-id }|.
      get_internal_types( CHANGING c_type = <h> ).
      <h>-type_definition = |types: t_{ <h>-name }{ <h>-id } type { <h>-absolute_type }.|.
      ELSEIF <h>-table EQ abap_true.
      <h>-final_type = |{ <h>-name } type tt_{ <h>-name }{ <h>-id }|.
      <h>-type_definition = |types: tt_{ <h>-name }{ <h>-id } type standard table of t_{ <h>-name }{ <h>-id } with default key.|.
      ELSE.

      get_internal_types( CHANGING c_type = <h> ).
      ENDIF.
      ENDLOOP.

      * LOOP AT hierarchy ASSIGNING <h> GROUP BY ( parent = <h>-parent ).
      * CLEAR components.
      * LOOP AT GROUP <h> ASSIGNING FIELD-SYMBOL(<g>).
      * components = components && <g>-final_type && ',' && cl_abap_char_utilities=>newline.
      * ENDLOOP.
      * ASSIGN hierarchy[ absolute_type = <h>-parent ] TO FIELD-SYMBOL(<parent>).
      * IF sy-subrc EQ 0.
      * REPLACE ALL OCCURRENCES OF c_components IN <parent>-type_definition WITH components.
      * ENDIF.
      * ENDLOOP.

      DATA(lt_hierarchy_parent) = hierarchy[].

      DATA: lv_parent_level TYPE i.
      LOOP AT hierarchy ASSIGNING <h>.
      CLEAR: lv_parent_level, components.

      lv_parent_level = <h>-level - 1.

      LOOP AT lt_hierarchy_parent ASSIGNING FIELD-SYMBOL(<g>) WHERE parent = <h>-parent AND level = <h>-level.
      components = components && <g>-final_type && ',' && cl_abap_char_utilities=>newline. 
      ENDLOOP.

      READ TABLE hierarchy ASSIGNING FIELD-SYMBOL(<parent>) WITH KEY absolute_type = <h>-parent level = lv_parent_level.
      IF sy-subrc EQ 0.
      REPLACE ALL OCCURRENCES OF c_components IN <parent>-type_definition WITH components."操作 父级
      ENDIF.
      ENDLOOP.

      SORT hierarchy BY level DESCENDING structure DESCENDING.

      LOOP AT hierarchy ASSIGNING <h> WHERE structure EQ abap_true
      OR table EQ abap_true.
      r_definition = r_definition && <h>-type_definition && cl_abap_char_utilities=>newline.
      ENDLOOP.
      ENDMETHOD.

       

      Here my testing json

      {
      	"header": {
      		"headtab": [{
      			"ico1637320005": "F012",
      			"ico1642010025": "202210",
      			"ico2241150095": "20221001",
      			"ico2241160099": "20221031"
      		}]
      	},
      	"content": {
      		"cta0904510024": [{
      			"tco0904510025": "02",
      			"tco0904510026": "300",
      			"tco0904510027": "2772.00",
      			"tco0904510028": "1630.40",
      			"tco0904590031": "02",
      			"tco0904590032": "text________________",
      			"tco0904590033": "2022-10-01",
      			"tco0904590034": "2022-10-31",
      			"tco0904590035": "456000",
      			"tco0904590036": "200000",
      			"tco0904590037": "0.6",
      			"tco0904590038": "153600.00",
      			"tco0904590039": "0.02",
      			"tco0906140040": "10",
      			"tco0906140041": "15092303",
      			"tco0906140042": "",
      			"tco0906140043": "0.3",
      			"tco0906140044": "831.60",
      			"tco0906140045": "300",
      			"tco1521290001": "text________________",
      			"tco1521340002": "F0121"
      		}, {
      			"tco0904510025": "",
      			"tco0904510026": "",
      			"tco0904510027": "",
      			"tco0904510028": "",
      			"tco0904590031": "",
      			"tco0904590032": "",
      			"tco0904590033": "",
      			"tco0904590034": "",
      			"tco0904590035": "",
      			"tco0904590036": "",
      			"tco0904590037": "",
      			"tco0904590038": "",
      			"tco0904590039": "",
      			"tco0906140040": "",
      			"tco0906140041": "",
      			"tco0906140042": "",
      			"tco0906140043": "",
      			"tco0906140044": "",
      			"tco0906140045": "",
      			"tco1521290001": "text________________",
      			"tco1521340002": "F0122"
      		}, {
      			"tco0904510025": "",
      			"tco0904510026": "",
      			"tco0904510027": "",
      			"tco0904510028": "",
      			"tco0904590031": "",
      			"tco0904590032": "",
      			"tco0904590033": "",
      			"tco0904590034": "",
      			"tco0904590035": "",
      			"tco0904590036": "",
      			"tco0904590037": "",
      			"tco0904590038": "",
      			"tco0904590039": "",
      			"tco0906140040": "",
      			"tco0906140041": "",
      			"tco0906140042": "",
      			"tco0906140043": "",
      			"tco0906140044": "",
      			"tco0906140045": "",
      			"tco1521290001": "text________________",
      			"tco1521340002": "F0124"
      		}, {
      			"tco0904510025": "02",
      			"tco0904510026": "300",
      			"tco0904510027": "5700.00",
      			"tco0904510028": "3480.00",
      			"tco0904590031": "02",
      			"tco0904590032": "text________________",
      			"tco0904590033": "2022-10-01",
      			"tco0904590034": "2022-10-31",
      			"tco0904590035": "600000",
      			"tco0904590036": "100000",
      			"tco0904590037": "0.6",
      			"tco0904590038": "300000.00",
      			"tco0904590039": "0.02",
      			"tco0906140040": "10",
      			"tco0906140041": "",
      			"tco0906140042": "",
      			"tco0906140043": "0.3",
      			"tco0906140044": "1710.00",
      			"tco0906140045": "500",
      			"tco1521290001": "text________________",
      			"tco1521340002": "F0123"
      		}],
      		"sum": {
      			"cta0904510024_sum": {
      				"tco0904510025": "",
      				"tco0904510026": "600",
      				"tco0904510027": "8472",
      				"tco0904510028": "5110.4",
      				"tco0904590031": "",
      				"tco0904590032": "",
      				"tco0904590033": "",
      				"tco0904590034": "",
      				"tco0904590035": "1056000",
      				"tco0904590036": "300000",
      				"tco0904590037": "",
      				"tco0904590038": "453600",
      				"tco0904590039": "",
      				"tco0906140040": "20",
      				"tco0906140041": "",
      				"tco0906140042": "0",
      				"tco0906140043": "0.6",
      				"tco0906140044": "2541.6",
      				"tco0906140045": "800",
      				"tco1521290001": "",
      				"tco1521340002": ""
      			}
      		}
      	}
      }
      Author's profile photo Łukasz Pęgiel
      Łukasz Pęgiel
      Blog Post Author

      Thanks for the info!
      Would you be so kind and create the pull request with the needed changes? https://github.com/fidley/JSON2ABAPType

      Cheers
      Łukasz

      Author's profile photo Haorong Liang
      Haorong Liang

      Opps..I'm afraid Im not able to pull the code into the github due to the network setting by our security team. Pulling above code or not, all depends on your own willing now, go ahead~ 🙂