How to work with JSON messages


I not find any example or instruction to work with JSON in Akka Streams.

My case is very simple.

Kafka is a Source, with JSON messages, one per message, like {“a”:1}.

Make simple operations with Flow, add new field and get a field from JSON and make basic math.

Kafka is a Sink too, send result in JSON.

How do this?

If the messages are “valid” jsons;

  • Get your favorite json lib like spray-json or circe or whatever you want.
  • add some simple .map(parseJson).map(modifyJson).map(serializeJson) to your stream
  • implement the functions as you (or your json lib) want :)

If the content really a stream like {size: 120000, data: [{id: 1, ....}, {...}, ...., {....}]} its a bit trickier to chunk and parse. But akka-http has marshallers for this use-case, I think they can parse this kind of input too. (If this is the case and you are still stucked I will do a better research with links, but based on what you wrote I think these are selfcontained jsons and not a single large json.)

1 Like

Is ok to use play-json (

Yes, you may use anything in the map operators.


Thx a lot!