How to send a JSON message with RabbitMQ (Python pika)
Codemia Knowledge Hub ·
Serialize the data to a JSON string with json.dumps and publish that string as the message body. RabbitMQ just carries the bytes; the producer and consumer agree that they are JSON. Below is a complete example in Python with the pika library.
The AMQP pieces involved
RabbitMQ follows the AMQP (Advanced Message Queuing Protocol) model:
- Producer: an application that sends messages.
- Queue: a buffer that stores messages.
- Exchange: routes messages to one or more queues based on routing rules.
- Binding: a link between a queue and an exchange.
- Consumer: an application that receives messages.
Sending JSON with pika
Install pika
With a RabbitMQ server running locally or reachable remotely:
Connect
Create a blocking connection to the broker and open a channel on it:
Serialize the message
Import the json module and turn the dictionary into a JSON string:
Declare the exchange and queue
Declare both before sending, then bind the queue to the exchange with a routing key:
Publish
Publish the JSON string through the exchange with the same routing key, then close the connection:
| Component | Description | Sample Code or Value |
| Producer | Sends messages to an exchange. | - |
| Exchange Type | Types include direct, topic, headers, fanout. | 'direct' |
| Message Format | Format of the message being sent. | JSON serialized string |
| Queue | Stores messages until consumed. | 'test_queue' |
| pika | Python library to interact with RabbitMQ. | import pika |
Before using this in production
- Errors and logging: handle disruptions such as network failures, and log deliveries and failures for diagnostics.
- Durability and acknowledgements: configure message acknowledgements and queue durability to match your reliability needs so messages are not lost.
- Security: enable SSL/TLS so data in transit is protected, especially when it is sensitive.
Related reading
- RabbitMQ - upgraded to a new version and got a lot of PRECONDITION_FAILED unknown delivery tag 1
- RabbitMQ - vhost '/' is down for user 'XYZ'. even after user has all access
- RabbitMQ - Wildcard in routing key vs binding key
- RabbitMQ 3.3.1 can not login with guest/guest
- RabbitMQ 3.5 and Message Priority
- RabbitMQ 3.6.1 / Erlang 18.3 TLS insufficient security failures
- RabbitMQ / ActiveMQ or Redis for over 250,000 msg/s
- RabbitMQ / AMQP single queue, multiple consumers for same message?
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.