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

Convert json to abap structure

venk123
Explorer
0 Likes
2,914

Hi All

I'm trying to convert a JSON File into ABAP Deep entity table

JSON File Format

{  "Orders": {
    "Order": [
      {
        "Customer": "Test1",
        "SaleType": "",
        "Item Nav": [
          {
            "ItmNumber": "1",
            "Material": "11",            
          },
          {
            "ItmNumber": "2",
            "Material": "22",
          }
        ]
       
      },
      {
        "Customer": "Test2",
        "SaleType": "",
       
        "Item Nav": [
          {
            "ItmNumber": "1",
            "Material": ""
          },
          {
            "ItmNumber": "2",
            "Material": ""
          }
        ]
        
      }

    ]

  }
}
TYPES : BEGIN OF ts_sales_item_data,<br>          ItmNumber TYPE char5,<br>          Material  TYPE char5,<br>        END OF ts_sales_item_data.
TYPES: BEGIN OF ty_deep_entity,
customer TYPE char10,
 saletype TYPE vkorg,
 headertoitem TYPE TABLE OF ts_sales_item_data WITH DEFAULT KEY,
 END OF ty_deep_entity.


DATA: it_result TYPE TABLE OF ty_deep_entity,
  <br> wa_result TYPE ty_deep_entity.
  <br>CALL METHOD cl_fdt_json=>json_to_data
  <br> EXPORTING
  <br> iv_json = result
  <br> CHANGING
  <br> ca_data = wa_result.
  
For some reason - I don't see any records into wa_result.
Any suggestions would be appreciated
Thanks in advance

Hi All

I'm trying to convert a JSON File into ABAP Deep entity table

JSON File Format

{  "Orders": {
    "Order": [
      {
        "Customer": "Test1",
        "SaleType": "",
        "Item Nav": [
          {
            "ItmNumber": "1",
            "Material": "11",            
          },
          {
            "ItmNumber": "2",
            "Material": "22",
          }
        ]
       
      },
      {
        "Customer": "Test2",
        "SaleType": "",
       
        "Item Nav": [
          {
            "ItmNumber": "1",
            "Material": ""
          },
          {
            "ItmNumber": "2",
            "Material": ""
          }
        ]
        
      }

    ]

  }
}
TYPES : BEGIN OF ts_sales_item_data,<br>          ItmNumber TYPE char5,<br>          Material  TYPE char5,<br>        END OF ts_sales_item_data.
TYPES: BEGIN OF ty_deep_entity,
customer TYPE char10,
 saletype TYPE vkorg,
 headertoitem TYPE TABLE OF ts_sales_item_data WITH DEFAULT KEY,
 END OF ty_deep_entity.


DATA: it_result TYPE TABLE OF ty_deep_entity,
  <br> wa_result TYPE ty_deep_entity.
  <br>CALL METHOD cl_fdt_json=>json_to_data
  <br> EXPORTING
  <br> iv_json = result
  <br> CHANGING
  <br> ca_data = wa_result.
  
For some reason - I don't see any records into wa_result.
Any suggestions would be appreciated
Thanks in advance
4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Likes
2,564

It's not valid JSON format. For instance, it would be valid if you wrap it between { and }.

Read only

venk123
Explorer
0 Likes
2,564

Sandra - Does that look right now ..Even than i'm having issues..Is there an issue in ABAP Code

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,564

The type of the argument of parameter CA_DATA doesn't reflect the JSON structure. There is "Order" which is an array of customers, but the type of your argument contains only one customer. I don't see how JSON_TO_DATA can find the relationship between the JSON structure and the type of the argument.

Read only

ThorstenHoefer
Active Contributor
0 Likes
2,564

Hi,

please chck the Method

/ui2/cl_json=>deserialize(

You can also check the reverse way to serialized your table structure to JSON

to check the correct syntax.

The JSON type SalesType will be transferred to the abap field sales_type

with the camel case mode:

  lv_json = /ui2/cl_json=>serialize( 
data = it_result
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
compress = abap_true ).