0

My JSON file : (It's a file)

{ "user-id": 10009, "rating": 3, "movie_id": 9823 }

I need to get the each data separately.so I can store them in database. the JSON file is a form-data.

I tried:

    def post(self, request):

        data = request.FILES['json_file']
        # a = data.read().decode('utf-8')
        a = json.loads(data)
        x = a['user-id']
        print(x)

    return Response(x, status=status.HTTP_201_CREATED)

The above code is not working and giving me error:

the JSON object must be str, not 'InMemoryUploadedFile'

How can i get data from JSON file(form-data) and store it's content in database?

enter image description here

Zodiac
  • 121
  • 1
  • 9

1 Answers1

0

data is of the type InMemoryUploadedFile, so to convert it to a string you need to read it (as bytes) and convert it to a string:

 def post(self, request):

        data = request.data.get('json_file')
        a = json.loads(str(data.read()))
        x = a['user-id']
        print(x)

    return Response(x, status=status.HTTP_201_CREATED)


SteveK
  • 995
  • 5
  • 17