kafka console consumer pretty print json
Asked Answered
A

2

5

How to print response from Kafka console consumer to JSON pretty. I have tried this but it's not working.

./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic indexing-v1 
--from-beginning | python -m json.tool
Amen answered 4/3, 2020 at 12:12 Comment(0)
A
8

Using jq was able to view pretty JSON. Thanks.

./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic indexing-v1 
--from-beginning  | jq 
Amen answered 4/3, 2020 at 12:16 Comment(1)
it working for curl response but for what reason; not for Kafka consumer.Amen
C
0

To use python -m json.tool you must add --json-lines option, as stream of JSON objects separated by newlines is not a valid JSON, but jsonlines.

$ echo '{"a":2,"3":4}\n{"a":2,"3":4}' | python -m json.tool
Extra data: line 2 column 1 (char 14)
$ echo '{"a":2,"3":4}\n{"a":2,"3":4}' | python -m json.tool --json-lines
{
    "a": 2,
    "3": 4
}
{
    "a": 2,
    "3": 4
}

With pygments you can also use ./kafka-console-consumer.sh ... | python -m json.tool --json-lines | pygmentize -s -l json.

Cutin answered 19/6 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.