Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine values under the same key from a list of key-value pairs #3867

Closed
Tracked by #3967
engechas opened this issue Dec 28, 2023 · 6 comments · Fixed by #4038
Closed
Tracked by #3967

Combine values under the same key from a list of key-value pairs #3867

engechas opened this issue Dec 28, 2023 · 6 comments · Fixed by #4038
Labels
plugin - processor A plugin to manipulate data in the data prepper pipeline.

Comments

@engechas
Copy link
Collaborator

engechas commented Dec 28, 2023

Is your feature request related to a problem? Please describe.
As a user, I would like to have the ability to transform a map to a list, or flatten a list of maps to a list.

Example input:

{
  "mylist": [
    { "a": "b" },
    { "a": "c" },
    { "x": "y" },
  ]
}

Example output:

{
  "a": ["b", "c"],
  "x": ["y"]
}

Describe the solution you'd like
A new processor created to implement the above described functionality

@dlvenable dlvenable added plugin - processor A plugin to manipulate data in the data prepper pipeline. and removed untriaged labels Jan 2, 2024
@dlvenable
Copy link
Member

@engechas , I'm a bit unsure how we have the first example to begin with.

"a": "b",
"a": "c",

There should be only one a in the incoming Event. How do we have multiple a keys?

@engechas
Copy link
Collaborator Author

engechas commented Jan 8, 2024

You're right, the first one would not be possible. I've removed it

@oeyh
Copy link
Collaborator

oeyh commented Jan 8, 2024

The example given looks like what the ListToMap processor can handle with some changes, or a new processor that aggregates objects inside an array.

I created another issue for "MapToList" processor that performs approximately the reversed operation of the ListToMap processor.

@oeyh oeyh changed the title Map to list processor Combine values under the same key from a list of key-value pairs Jan 16, 2024
@oeyh
Copy link
Collaborator

oeyh commented Jan 24, 2024

If I can simplify the request a bit so that the objects in mylist all have same keys:
input:

{
  "mylist": [
    { "a": "b" },
    { "a": "c" },
    { "a": "y" },
  ]
}

output:

{
  "a": ["b", "c", "y"]
}

this could already help with the original user request (from an internal ticket) as well as #3963 and #3964.

We can achieve this transformation with a small change to the existing list_to_map processor:

  • Make key an optional config. When key is not specified, use value_key literally as the key in the result map.

The processor config for the above example would look like this:

  processor:
    - list_to_map:
        value_key: "a"
        source: "mylist"

@dlvenable
Copy link
Member

@oeyh , The original ask in this issue is to get the key from the inner map and then combine the values together.

I do think this can be achieved by using the existing list_to_map processor. But, it seems we are looking for more of a dynamic target key. Maybe a boolean would be most appropriate?

- list_to_map:
    source: mylist
    use_source_key: true

@oeyh
Copy link
Collaborator

oeyh commented Jan 29, 2024

@dlvenable My only concern with the above approach is that the behavior for value_key would change. value_key specifies the key to extract value; if not specified, we take the entire object as value (see #2410). We would get result like this:

{
  "a": [{ "a": "b" }, { "a": "c" }]
  "x": [{ "x": "y" }]
}

But we can probably add another boolean flag extract_value to go with use_source_key?

- list_to_map:
    source: mylist
    use_source_key: true
    extract_value: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin - processor A plugin to manipulate data in the data prepper pipeline.
Projects
Development

Successfully merging a pull request may close this issue.

3 participants