0
votes

My json file looks like :

{
    "id": 1,
    "name": "A green door",
    "price": 12.50,
    "tags": ["home", "green"]
}

I am using the following boto code to upload this to Amazon SQS

import json
import uuid
import time
import boto.sqs
import boto
import glob
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
from boto.sqs.message import RawMessage


def process_file(json_file):
        sqs = boto.sqs.connect_to_region("ap-southeast-1")
        queue = sqs.get_queue("Killswitch")
        with open(json_file) as json_fileone:
                dataone=json.load(json_fileone)
                print dataone
                [queue.write(queue.new_message(i)) for i in dataone]
                print "File sent successfully to queue"

json_files = glob.glob("*json")
for json_file in json_files:
    process_file(json_file)

This code works fine, but when I check my SQS queue via AWS console on browser, I see 4 messages instead of one single message consisting on this json file..

And when I try to read the same via boto code I end up receiving the following :

id 
name 
price 
tags

I need to upload the whole file in one single message and not as 4 objects/messages.

1

1 Answers

2
votes

It is because in this line:

[queue.write(queue.new_message(i)) for i in dataone]

for i in dataone means you are iterating the keys of the json objects

You are effectively doing

queue.write(queue.new_message('id')) 
queue.write(queue.new_message('name')) 
queue.write(queue.new_message('price')) 
queue.write(queue.new_message('tags')) 

Apparently the API only accepts string, you can try:

    with open(json_file) as json_fileone:
            dataone=json_fileone.read()
            queue.write(queue.new_message(dataone))
            print "File sent successfully to queue"