0

I am having a problem while trying to build a JSON response from my server.

I want to get a JSON object that contains other JSON objects which I get as a result from SQL query so I would be able to send the containing JSON via my websocket server

Until now I tried something like this:

    def screenColor(self,ne_Lat, ne_Lng, sw_lat, sw_Lng):
    data={}
    allData=[]
    for rec in self.c.execute('''SELECT * FROM squares WHERE ((lat BETWEEN ? AND ?) AND (long BETWEEN ? AND ?)) ''',(sw_lat, ne_Lat, sw_Lng, ne_Lng)):
          data['color']=rec[3]
          data['lat']=rec[1]
          data['lng']=rec[2]
          json_data=json.dumps(data)
          allData.append(json_data)
    return allData

and when I print the returned value i get:

['{"color": 85, "lat": 31.776879500000156, "lng": 35.21187200000153}', '{"color": 85, "lat": 31.778179500000157, "lng": 35.21187200000153}', '{"color": 85, "lat": 31.779479500000157, "lng": 35.21187200000153}', '{"color": 85, "lat": 31.780779500000158, "lng": 35.21187200000153}', '{"color": 85, "lat": 31.782079500000158, "lng": 35.21187200000153}', '{"color": 85, "lat": 31.78337950000016, "lng": 35.21187200000153}', '{"color": 26, "lat": 31.78467950000016, "lng": 35.21187200000153}', '{"color": 28, "lat": 31.78597950000016, "lng": 35.21187200000153}', '{"color": 85, "lat": 31.78727950000016, "lng": 35.21187200000153}', '{"color": 85, "lat": 31.776879500000156, "lng": 35.21367200000153}', '{"color": 28, "lat": 31.778179500000157, "lng": 35.21367200000153}', '{"color": 85, "lat": 31.779479500000157, "lng": 35.21367200000153}', '{"color": 26, "lat": 31.780779500000158, "lng": 35.21367200000153}', '{"color": 26, "lat": 31.782079500000158, "lng": 35.21367200000153}', '{"color": 26, "lat": 31.78337950000016, "lng": 35.21367200000153}', '{"color": 85, "lat": 31.78467950000016, "lng": 35.21367200000153}

Is there is a way to get this array as a JSON object? Is that already a JSON object?

Thank you very much!

2
  • make nested object and json.dumps() Commented Feb 1, 2017 at 15:35
  • Currently you have a list of strings (in JSON format), you just have to add the data dicts (as dicts, not as JSON) to allData and then execute json.dumps (allData) at the end Commented Feb 1, 2017 at 15:41

1 Answer 1

1

You should try the following:

def screenColor(self,ne_Lat, ne_Lng, sw_lat, sw_Lng):

    allData=[]
    for rec in self.c.execute('''SELECT * FROM squares WHERE ((lat BETWEEN ? AND ?) AND (long BETWEEN ? AND ?)) ''',(sw_lat, ne_Lat, sw_Lng, ne_Lng)):
          data = {}
          data['color']=rec[3]
          data['lat']=rec[1]
          data['lng']=rec[2]

          allData.append(data)
    return json.dumps(allData)

Otherwise, if you define your dictionary data outside the loop, it will be overwritten at every iteration I guess...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.