Search code examples
jsonapache-kafkaapache-kafka-streams

Read JSON data values


For example, if the data in kafka toipc looks like this

{
        "header": {
            "name": "jake"

        },
        "body": {
            "Data":"!#$%&&"
        }
}

So how do I read the value "!#$%&&" from my consumer application? I need to process the data once I get that data


Solution

  • You'll need to consume the data using String Serde, JSON Serde, or define your own.

    If you define your own, then you'd call value.getBody().getData(), like any other Java Object, where value is the argument from mapValues, peek, filter, etc. Kafka Streams DSL

    For the others, the answer will depend on what JSON library you're using, but the answer isn't unique to Kafka, so read that library's documentation on parsing strings.

    Here's one example of consuming using String Serde - https://github.com/confluentinc/kafka-streams-examples/blob/7.1.1-post/src/main/java/io/confluent/examples/streams/JsonToAvroExample.java#L118