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

Flatten json processor #4128

Closed
oeyh opened this issue Feb 14, 2024 · 4 comments · Fixed by #4138
Closed

Flatten json processor #4128

oeyh opened this issue Feb 14, 2024 · 4 comments · Fixed by #4138
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@oeyh
Copy link
Collaborator

oeyh commented Feb 14, 2024

Is your feature request related to a problem? Please describe.
There're use cases where nested json objects needs to be flattened. For example:
From:

{
  "key1": "val1",
  "key2": {
    "key3": {
      "key4": "val2"
    }
  },
  "list1": [
    {
      "list2": [
        {
          "name": "name1",
          "value": "value1"
        },
        {
          "name": "name2",
          "value": "value2"
        }
      ]
    }
  ]
}

to

{
  "key1": "val1",
  "key2.key3.key4": "val2",
  "list1[0].list2[0].name": "name1",
  "list1[0].list2[0].value": "value1",
  "list1[0].list2[1].name": "name2",
  "list1[0].list2[1].value": "value2",
}

In some specific use cases (#3965), the users wants to remove list indices from the flattened keys and combine leaf values in lists:

{
  "key1": "val1",
  "key2.key3.key4": "val2",
  "list1[].list2[].name": ["name1","name2"],
  "list1[].list2[].value": ["value1","value2"]
}

Describe the solution you'd like
A new flatten processor with these configurations:

  processor:
    - flatten:
        source: ""   
        target: ""  
        remove_processed_fields: true
        remove_list_indices: false
  • source: the source key to the object to flatten
  • target: the target key to put the flatten object
  • remove_processed_fields: boolean, whether to remove processed fields, only keeping flattened fields
  • remove_list_indices: boolean, whether to remove list indices from the flattened keys and combine leaf values

Describe alternatives you've considered (Optional)
N/A

Additional context
A specific use case is in #3965

@oeyh oeyh added the untriaged label Feb 14, 2024
@oeyh oeyh self-assigned this Feb 14, 2024
@oeyh oeyh added enhancement New feature or request and removed untriaged labels Feb 14, 2024
@oeyh oeyh mentioned this issue Feb 16, 2024
4 tasks
@kkondaka
Copy link
Collaborator

@oeyh This does not mean we should take the suggestion literally. I do not think we should have "[]" in keys. I serves no purpose.

@dlvenable dlvenable added this to the v2.7 milestone Mar 6, 2024
@timo-mue
Copy link

Sorry if this is the wrong place to ask, since this issue is already closed and the processor implemented, but what exactly is the purpose of having the empty squared brackets as part of the key when remove_list_indices is true?
We use the flatten processor with this option and for our usecase it would be much better and I think in general more intuitive if we had "list1.list2.name": ["name1","name2"] instead of "list1[].list2[].name": ["name1","name2"]

@oeyh
Copy link
Collaborator Author

oeyh commented Jun 10, 2024

@timo-mue It was based on a use case in #3965. Feel free to open an issue if you have different use cases and requests.

@timo-mue
Copy link

@timo-mue It was based on a use case in #3965. Feel free to open an issue if you have different use cases and requests.

thanks for your answer. I opened a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

4 participants