Search code examples
pythonpython-2.7rabbitmqpika

Consume message from RabbitMQ as list using pika in Python


I have a list like below in RabbitMq queue

[{'id':'10','url':'https://www.google.co.in/search?q=rabbitmq&oq=rabbitmq'},{'id':'11','url':'https://www.google.co.in/search?q=python&oq=python'}]

while consuming this message, I am getting this message like below as string but not as list

"[{'id':'10','url':'https://www.google.co.in/search?q=rabbitmq&oq=rabbitmq'},{'id':'11','url':'https://www.google.co.in/search?q=python&oq=python'}]"

I tried to convert this string to list using ast.literal_eval(my_list) but getting SyntaxError: EOL while scanning string literal

How can I get/convert this RabbitMQ message as list?


Solution

  • Here is the steps to do that:

    1. Use double quote for json array "

    2. Use json module

    import json

    text = "[{'id':'10','url':'https://www.google.co.in/search?q=rabbitmq&oq=rabbitmq'},{'id':'11','url':'https://www.google.co.in/search?q=python&oq=python'}]"

    text2 = text.replace("'", '"')

    print json.loads(text2)