Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Splitting json message based on the matching field name #2251

Closed
sesa657547 opened this issue Nov 20, 2023 · 2 comments
Closed

Splitting json message based on the matching field name #2251

sesa657547 opened this issue Nov 20, 2023 · 2 comments
Labels

Comments

@sesa657547
Copy link

Hi,

I am new to benthos and on the process of learning it. I have got a situation where I need to split the single payload into multiple messages based on the field names and send these messaged to different kafka topics. My payload looks like this:

[
{
"dId": "kjglsjglfkgfdkgfhjfhdsj",
"d1": "fhkfjsaf",
"d2": {
"dc1": 8,
"dc2": 9
},
"timestamp": "2020-09-03",
"A": [
{
"aId": "jflksflskgskg",
"af1": "kkfls",
"af2": {
"ac1": 6,
"ac2": 2
},
"af3": 1,

      "af4": "00",
      "af5": 4
    }
  ],
  "B": [
    {
      "bId": "jgljgldjgs",
      "bf1": 6,
      "bf2": {
        "bc1": 4,
        "bc2": 2
      }
    }
  ],
  "C": [
    {
      "cId": "njfglsjg",
      "cf1": 3,
      "cf2": "jfgg"
    }
  ]
}

]

I want to send A array data to one kafka topics and similarly for B and C.
Thanks
M

@mihaitodor
Copy link
Collaborator

Hey @sesa657547 👋 Here's how I'd do it:

input:
  generate:
    count: 1
    mapping: |
      root = [
        {
          "dId": "kjglsjglfkgfdkgfhjfhdsj",
          "d1": "fhkfjsaf",
          "d2": {
            "dc1": 8,
            "dc2": 9
          },
          "timestamp": "2020-09-03",
          "A": [
            {
              "aId": "jflksflskgskg",
              "af1": "kkfls",
              "af2": {
                "ac1": 6,
                "ac2": 2
              },
              "af3": 1,
              "af4": "00",
              "af5": 4
            }
          ],
          "B": [
            {
              "bId": "jgljgldjgs",
              "bf1": 6,
              "bf2": {
                "bc1": 4,
                "bc2": 2
              }
            }
          ],
          "C": [
            {
              "cId": "njfglsjg",
              "cf1": 3,
              "cf2": "jfgg"
            }
          ]
        }
      ]
  processors:
    - mapping: |
        root = this.0.with("A", "B", "C").key_values()
    - unarchive:
        format: json_array
    - mapping: |
        meta key = this.key

        root = this.value

output:
  kafka:
    # ...
    topic: ${! @key }
    # ...

I used the generate input to show you how you can test this payload as a standalone example.

If A, B and C aren't the actual topic names, you can massage them further using bloblang inside ${! ... }, or if you just need to prepend / append some string to them, you can also do stuff like foo-${! @key }-bar.

@Jeffail
Copy link
Collaborator

Jeffail commented Nov 20, 2023

Moving to discussion (#2026)

@redpanda-data redpanda-data locked and limited conversation to collaborators Nov 20, 2023
@Jeffail Jeffail converted this issue into discussion #2252 Nov 20, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

3 participants