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: 

Send Few key names in json though those are intial in case of compress as true

RachamallaKiran
Participant
1,916

hi all,

I am calling

r_json = NEW /ui2/cl_json( pretty_name = /ui2/cl_json=>pretty_mode-camel_case

compress = abap_true )->serialize_int( data = is_grid_data ).

where compress flag is true due to which if there is any field value with intial it is not getting added in json.

but is there any option where we can tell the method though that is initial it must get added in json.

Here is a example

types: begin of ty_test,

abc type c,

tab1 type string-table,

val2 type string,

col3 type n,

end of ty_test.

here though tab1 is inital i want that to be added in json

is there any opion i can tell that tab1 should be there in json though it is inital.

1 ACCEPTED SOLUTION

alexey_arseniev
Product and Topic Expert
Product and Topic Expert
1,724

Hi Kiran,

if you want to have a custom logic for compressing only some fields you need to extend the class and overwrite the IS_COMPRESSIBLE method.

More info and example can be found here: abap-to-json/docs/class-extension.md at main · SAP/abap-to-json (github.com)

BR, Alex.

8 REPLIES 8

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
1,724

One ugly way to do this is that you put some special unique constant value inside that variable.

Lets say:

CONSTANTS: lc_dummy_value TYPE string VALUE '[DUMMY_DELETE_ABAP_VALUE]'.

And than later find it in result JSON string and delete it.

In your case it would be little bit more complicated because you have not a simple field but array (itab). So you will probably need to delete also some JSON brackets around the constant to make tab1 initial in JSON...

-- Tomas --

0 Kudos
1,724

Any Better solution is appreciated.

Actually we have 2 complex structures which involves lot of tables structures and strings integers.

0 Kudos
1,724

kiran.rachamalla Other solution could be creation of multiple JSONs (one without compress, one with compress etc.) and then combine them together. But that is also not ideal.

Another way is you can explore JSON creation using TRANSFORMATION. But I do not have much experience with it. So I am not sure if it will work for you. You can find blogs and resources online.

For example here is nice blog with links to other sources:

https://blogs.sap.com/2022/10/27/abap-fast-json-serialization/

-- Tomas --

1,724

The Problem is Even the fields with boolean flags are also not getting when compressed flag is set true due to which false flag is not going to UI. there are lot of boolean flags in my json.

Regards,

kiran rachamalla.

Sandra_Rossi
Active Contributor
0 Kudos
1,724

Problem with formatting of your question. This is better:

r_json = NEW /ui2/cl_json( pretty_name = /ui2/cl_json=>pretty_mode-camel_case
compress = abap_true )->serialize_int( data = is_grid_data ).

where compress flag is true due to which if there is any field value with intial it is not getting added in json.

but is there any option where we can tell the method though that is initial it must get added in json.

Here is a example

types: begin of ty_test,
abc type c,
tab1 type string-table,
val2 type string,
col3 type n,
end of ty_test.

here though tab1 is inital i want that to be added in json

is there any opion i can tell that tab1 should be there in json though it is inital.

Sandra_Rossi
Active Contributor
0 Kudos
1,724

Out of curiosity, why is it needed to generate these few properties with an initial value? Also, are these string, number, boolean, null, object or array properties?

alexey_arseniev
Product and Topic Expert
Product and Topic Expert
1,725

Hi Kiran,

if you want to have a custom logic for compressing only some fields you need to extend the class and overwrite the IS_COMPRESSIBLE method.

More info and example can be found here: abap-to-json/docs/class-extension.md at main · SAP/abap-to-json (github.com)

BR, Alex.

0 Kudos
1,724

I think this works out. Thanks