Python removes extra key from result

Hi I am experimenting with python and mongodb with tornado framework. I have an input module where a user can insert data of students in academic and sports fields. I am using PostMan to inspect 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 fine. But the problem is with how the output is drawn.

The result looks like this

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

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

      

Due to the presence of two data, I could not get the values ​​for Name, Total, Reg.

Is there any way I can send [{"_id": "55acc2205d8882ef8a667d34", "Reg": "11mt", "Name": "Alex", "Total": "98"}]

'null' instead.

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

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

I don't know how this will happen.

0


source to share





All Articles