pythonjsonkafka-python

How to format unconventional output into simple json dump


In the answer from this post: https://stackoverflow.com/a/55832055/5937760

The output is:

{TopicPartition(topic=u'python-test', partition=0): 5,
 TopicPartition(topic=u'python-test', partition=1): 20,
 TopicPartition(topic=u'python-test', partition=2): 0}

When I do a json.dumps on it, I get this:

[["python-test", 0], ["python-test", 1], ["python-test", 2]]

How can I get it to output like so:

[[0,5],[1,20],[2,0]]

Solution

  • The value looks like a dictionary, so you should be able to get its keys and values with .items(). Then extract the partition number from the key.

    json.dumps([[key.partition, value] for key, value in end_offset.items()])