How to return Json object not stringified in django view from a request out to openai

I have the following code in a Django view in my API, this line in particular returns stringified in my api view

course_title = openai.Completion.create(
                engine="davinci", prompt=course_title_grab, max_tokens=5)
class CreateCourseView(APIView):
    serializer_class = CreateCourseSerializer

    def post(self, request, format=None):
        if not self.request.session.exists(self.request.session.session_key):
            self.request.session.create()

        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            course_title_grab = serializer.data.get('course_title')

            course_title = openai.Completion.create(
                engine="davinci", prompt=course_title_grab, max_tokens=5)

            course_topic = serializer.data.get('course_topic')
            course_topic_about = serializer.data.get('course_topic_about')
            course_question = serializer.data.get('course_question')
            course_answer = serializer.data.get('course_answer')

            course = Course(course_title=course_title, course_topic=course_topic,
                            course_topic_about=course_topic_about, course_question=course_question, course_answer=course_answer)
            course.save()

            return Response(CourseSerializer(course).data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

what the object looks like

{
    "id": 5,
    "code": "BIWJDL",
    "course_title": "{\n  \"choices\": [\n    {\n      \"finish_reason\": \"length\",\n      \"index\": 0,\n      \"logprobs\": null,\n      \"text\": \" i lived in a new\"\n    }\n  ],\n  \"created\": 1639249703,\n  \"id\": \"fdsf\",\n  \"model\": \"davinci:2020-05-03\",\n  \"object\": \"text_completion\"\n}",
    "course_topic": "test",
    "course_topic_about": "test",
    "course_question": "test",
    "course_answer": "test",
    "created_at": "2021-12-11T19:08:23.476059Z",
    "updated_at": "2021-12-11T19:08:23.476120Z"
}

Ideally I would like the course_title to output as a nested object so I can easily traverse it in my front end.

Any help would be greatly appreciated.

Back to Top