0

Hi I am experimenting with python and mongodb with tornado framework. I am having entry module where user can insert the data of students in academic and sports field. I am using PostMan to verify the code.

def post(self):
    print(json_encode(self.body))
    db.SearchLog.insert(self.body)
    default = 'None'
    Date = self.body.get('Date', default)
    Reg = self.body.get('Reg', default)
    Name = self.body.get('Name', default)
    cursor = db.studentDetails.find({'Reg': Reg, 'Name': Name})
    value = yield cursor.count()
    if value is 0:
        db.studentDetails.insert(self.body)

    else:
        self.write("{"'"success"'": 1, "'"data"'":[")
        for document in (yield cursor.to_list(length=100)):
            self.write(format(JSONEncoder().encode(document)))
            c = 1
            if (c < value):
                c+=1
                value = value - 1
                self.write(",")
        self.write("]}")

The code works ok. But the problem is with the way the output is displayed.

The output looks like this

{"success": 1, "data":[{"_id": "55acc2205d8882ef8a667d34", "Reg": "11mt", "Name": "Alex", "Total": "98"}]}

{"data": null, "status": "success"}

Because of the presence of two data I could not get the values Name ,Total, Reg.

Is there a way I can send [{"_id": "55acc2205d8882ef8a667d34", "Reg": "11mt", "Name": "Alex", "Total": "98"}] instead of 'null'.

My output should be like {"data":"[{"_id": "55acc2205d8882ef8a667d34", "Reg": "11mt", "Name": "Alex", "Total": "98"}]" , "status": "success"}

The additional {"data": null, "status": "success"} I dont know how this is coming.

Tony Roczz
  • 2,366
  • 6
  • 32
  • 59
  • What exactly do you want to send? Like if you could provide expected input and output it will be much clear. From where is `{"data": null, "status": "success"}` coming? – Obscure Geek Jul 20 '15 at 10:49
  • 2
    This all really comes down to you are "still" building your own JSON strings rather than serializing the native data structures like you were told to do before: http://stackoverflow.com/questions/31450221/python-how-to-remove-the-final-comma-in-json-string – Blakes Seven Jul 20 '15 at 10:54
  • @ObscureGeek I want the output as `{"data":[{"_id": "55acc2205d8882ef8a667d34", "Reg": "11mt", "Name": "Alex", "Total": "98"}], "status": "success"}` – Tony Roczz Jul 20 '15 at 11:16
  • @BlakesSeven I tried the instructions to send it to list but I couldnt figure out how to do it.This entire thing is new to me and I find things very difficult to understand and so I am unable to grasp things correctly and fast. – Tony Roczz Jul 20 '15 at 11:20
  • 2
    @TonyRoczz There is an "Edit" link on your question that you use to add information when people ask of it. Adding things in comments is very difficult for people to read. But my message still stands. You need to be building the data structure and not trying to build your own strings. See [Python Data Structures](https://docs.python.org/3.1/tutorial/datastructures.html) for an introduction – Blakes Seven Jul 20 '15 at 11:24
  • @BlakesSeven I have updated my [my earlier question](http://stackoverflow.com/questions/31450221/python-how-to-remove-the-final-comma-in-json-string) with the results and problem i met please see that and if you can please help me. Am sorry I am new to such forums not aware of what to do and what not to do. – Tony Roczz Jul 20 '15 at 11:53

0 Answers0