2

I am writing a new RFC callable function in ABAP which should be able to import a list of key-values mapping.

The RFC calling application will use Python with the PyRFC library.

I am unsure whether I should create a new custom data structure of if I can re-use an existing data structure.

The import argument should be able to contain a list of this:

('key1', ['key1val1', 'key1val2', ...])
('key2', ['key2val1', 'key2val2', ...])
....

If possible I would like to re-use an existing data structure.

One ugly hack would be to design the API like this: use a string and parse at as json. But this is a work-around which I would like to avoid.

I found the data structure WDY_KEY_VALUE but there the value is a string. I would need a structure where the value is a list of strings.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
guettli
  • 25,042
  • 81
  • 346
  • 663
  • 1
    Why don't you define it like a JSON? `[{"KEY":"key1","VALUES":["key1val1","]},{"KEY":"key2",…}...]` The ABAP APIs should be sufficient and it's a short code (for instance, there are limitations, element names must be in upper case). Maybe you answered my question by "ugly hack … would like to avoid" (I don't see that so ugly) – Sandra Rossi Oct 06 '18 at 08:54
  • @SandraRossi I am new to abap, I can't follow your comment. For each input parameter of my rfc function I need to provide a data type. Which type you think I should use? Do you mean STRING? Or is there a type JSON? – guettli Oct 06 '18 at 11:16
  • Just use string and then process it with [any of the methods](https://blogs.sap.com/2013/01/07/abap-and-json/) (transformations, sXML, etc) provided by ABAP. – Suncatcher Oct 06 '18 at 11:52
  • @Suncatcher I do not like the NoSQL hype very much. I think protocol buffers or SAP-RFC is better then fuzzy json string passing around. I can transfer the data between sap and pyRFC with a great optimized protocol. Putting everything into a string is possible, but is is not precise. If there is more data in the json than the receiver (abap function) consumes, then nobody will notice it. I like data structures and relational databases :-) – guettli Oct 06 '18 at 12:08
  • @guettli Yes a STRING type, then you use the JSON standard deserializer, something like `call transformation id source xml json_string result root = dobj.` It used to be reserved to XML but recognizes a JSON string automatically. – Sandra Rossi Oct 06 '18 at 12:14
  • 1
    I don't know an existing data structure corresponding to your need, but it's easy to create it and use it to type your parameter. – Sandra Rossi Oct 06 '18 at 12:18

2 Answers2

4

You can create a deep structure with KEY defined with type STRING and VALUE defined with type STRINGTAB.

enter image description here

Haojie
  • 5,665
  • 1
  • 15
  • 14
2

modelling such data is perfectly possible in ABAP DDIC:

  1. create table type z_t_values with row being built in type string

  2. create structure type z_s_key_values with fields key type string and values type z_t_values

  3. create table type z_t_key_values with row type z_s_key_values

now, the type z_t_key_values corresponds to your example input: it is a table of rows, each row contains a single key and a table of values

iPirat
  • 2,197
  • 1
  • 17
  • 30
  • I am new to ABAP and unsure if such fundamental things like a list of strings need to be created for every use case again. Since you tell me to do so, I guess it is the current best practice to create a new table type with only one column of type string for every use case. Looks strange to me, but if it is the current best practice in the abap-world, then I will do it (and not look for an existing table type which could get re-used). – guettli Oct 09 '18 at 07:43
  • it is sort of best practice to model your external interface parameters individually, so that a change in one interface doesn't lead to unwanted changes in other interfaces that re-use this parameter type. In your case, you might find a DDIC type that already contains what you need, but one day SAP has a great idea, changes the type somehow and your interface is broken. It is less likely with a somewhat generic list of list of strings, but the general principle stands. – Dirk Trilsbeek Oct 09 '18 at 08:11
  • you can use stringtab as posted by Haojie, which is an existing table type. Bat as Dirk said, you may want to change that in future, so you would only change that in one place. PS: in ABAP, there are many different String-like types, like character types of different lengths, with/without case differentiation with/without converters. So it depends on the use case – iPirat Oct 09 '18 at 09:18