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
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
Using jq was able to view pretty JSON. Thanks.
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic indexing-v1
--from-beginning | jq
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
.
© 2022 - 2024 — McMap. All rights reserved.