how to solve "detail": "JSON parse error - Extra data: line 8 column 3 (char 153)" error in django rest framework?

Viewed 155

I can create a new post with this:

 {
   "Number": 1,
   "name": "1005001697316642",
   "image": "https://",
   "description": "fffffffff",
   "price": "USD 23.43",
   "buy": "https://"
 }

but when i want to post multiple contents like this:

 {
   "Number": 1,
   "name": "1005001697316642",
   "image": "https://",
   "description": "fffffffff",
   "price": "USD 23.43",
   "buy": "https://"
 },
 {
   "Number": 2,
   "name": "1005002480978025",
   "image": "https://",
   "description": "dffdfdddddddddddddd",
   "price": "USD 0.89",
   "buy": "https://"
 }

i get this error:

HTTP 400 Bad Request

Allow: GET, POST, HEAD, OPTIONS

Content-Type: application/json

Vary: Accept { "detail": "JSON parse error - Extra data: line 8 column 3 (char 153)" }

image

serializers.py:

from rest_framework import serializers
from .models import product
 


class productSerializer(serializers.ModelSerializer):
    class Meta:
        model= product
        fields="__all__"
 

views.py:

from django.shortcuts import render
from .models import *
from rest_framework import viewsets
from .serializers import productSerializer
from rest_framework.parsers import JSONParser

class productviewset(viewsets.ModelViewSet):
    serializer_class=productSerializer 
    queryset=product.objects.all()

How can i solve this problem?

1 Answers

try this

to post multiple objects you need a list/array

https://www.w3schools.com/js/js_json_arrays.asp

[
  {
     "Number": 1,
     "name": "1005001697316642",
     "image": "https://",
     "description": "fffffffff",
     "price": "USD 23.43",
     "buy": "https://"
  },
  {
     "Number": 2,
     "name": "1005002480978025",
     "image": "https://",
     "description": "dffdfdddddddddddddd",
     "price": "USD 0.89",
     "buy": "https://"
   }
 ]
Related