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

json field name mapping

former_member837595
Discoverer
0 Likes
7,351

hello experts,

I currently have a json output where I should map abap deep structure field names to the expected json output.

ex: /XYZ/V_CRNCY -> VCRNCY. I'm currently using a REPLACE ALL REGEX to remove the /XYZ/ from each field but I think there should be a way to do this using the /ui2/cl_json class attributes.

In addition, I should return the values of each deep structure in a results array, remove client field from output and format dates so that there are no dashes where the data type is DATS

ex: "STRUCT_NAV": [

{

"MANDT": "200",

}

]

"STRUCT_NAV": {

"results": []

}

I've attached my structure and current json where I am attempting to use /ui2/cl_json class to serialize and output the json. I apologize if I haven't provided enough information and will be glad to provide more to receive some help.

sample.txt

1 ACCEPTED SOLUTION
Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
6,966

Hello Gregory,

  • For mapping different names JSON vs. ABAP you can use parameter NAME_MAPPINGS in DESERIALIZE method like you are using. But I am afraid you need to list all the possible field names. Not dynamic like you are trying to. /XYZ/AAA_TUVWX => AAA_TUVWX, /XYZ/ORG_INST_DT => ORG_INST_DT etc...
  • Using REPLACE might be also a option (just be careful not replacing something in values).
  • I do not understand how do you want to remove client field from which output? Is not it JSON string => ABAP structure?
  • If date is in correct JSON format, then /ui2/cl_json can convert it to SAP date format itself. Othervise you will have to do it in ABAP.
-- Tomas --
4 REPLIES 4
Read only

former_member27
Product and Topic Expert
Product and Topic Expert
0 Likes
6,966

Thank you for visiting SAP Community to get answers to your questions.

As you're looking to get the most out of your community membership, please consider including a profile picture to increase user engagement & additional resources to your reference that can really benefit you:

I suggest you use the "Insert Code option for the question.

Profile https://developers.sap.com/tutorials/community-profile.html

Tips for Questions: https://community.sap.com/resources/questions-and-answers

Consider taking our Q&A tutorial at: https://developers.sap.com/tutorials/community-qa.html.

I hope you find this advice useful, and we're happy to have you as part of the SAP Community!

All the best,

Dedi

Regards,
Dedi Metser
Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
6,967

Hello Gregory,

  • For mapping different names JSON vs. ABAP you can use parameter NAME_MAPPINGS in DESERIALIZE method like you are using. But I am afraid you need to list all the possible field names. Not dynamic like you are trying to. /XYZ/AAA_TUVWX => AAA_TUVWX, /XYZ/ORG_INST_DT => ORG_INST_DT etc...
  • Using REPLACE might be also a option (just be careful not replacing something in values).
  • I do not understand how do you want to remove client field from which output? Is not it JSON string => ABAP structure?
  • If date is in correct JSON format, then /ui2/cl_json can convert it to SAP date format itself. Othervise you will have to do it in ABAP.
-- Tomas --
Read only

0 Likes
6,966
Thanks for your reply Tomas.

- I understand about the NAME_MAPPINGS. Since the field names won't change listing them all individually would be tedious at first but it would handle the situation without using REPLACE ALL REGEX which could potentially change future values as well.

- The client field is in the ABAP structure that tells us which system ID we're currently using. ex: "MANDT" : 200. The table being pulled from being PQRSTU for example. The json itab I use to feed into the ui2/cl_json serialize method is of the same type as the parent table so it has all the same fields. I've been trying to find a simple way to remove that field.

- Would I use TS_AS_ISO8601 = abap_true to modify the date value from 2021-03-01 -> 20210301 ? This didn't seem to apply the result I needed

"PQRSTU": [
      {
        "MANDT": "200",
        "PNGUID": "",
        "PNCNT": 1,
        "PNTXCNT": 1,
        "VERSN": 3,

TYPES: BEGIN OF ty_json,
            
            XYZ_NAV TYPE /XYZ/GB_TT_PQRSTU,
            
           END OF ty_json.
	lt_json TYPE TABLE OF ty_json,
Read only

0 Likes
6,966

gregfranco

About MANDT field - I am still not sure what do you want to archieve. Remove MANDT field from ABAP structure to which you are deserializing JSON? Check example below, it works even if there is no MANDT field in ABAP structure.

About date + time, this works OK for me. No need to use TS parameter (you work with date, not timestamp).

TYPES: BEGIN OF ty_s_data,
         date TYPE d,
         time TYPE t,
       END OF ty_s_data.
DATA: ls_data TYPE ty_s_data,
      lv_act  TYPE string.

lv_act = '{"MANDT":"200","DATE":"2016-07-08","TIME":"12:34:56"}'.

/ui2/cl_json=>deserialize( EXPORTING json = lv_act CHANGING data = ls_data ).
-- Tomas --