Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

534
Vistas
Django python How to insert a json containing several objects in postgresql

I would like to insert a json containing several publications in my database. I want to create a field for each publication. For the moment I managed to insert a json containing a single publication, however when I try with two, it makes me a mistake:

Script python :

fichier = open("result2.json","r")
filedata = json.load(fichier)
#for '{' in filedata:
   #print "ici un {"
prod = Production()
prod.cle = filedata.get("UT")
prod.titre = filedata.get("TITRE")
prod.publis = filedata
prod.maj = timezone.now()
prod.save()
fichier.close()

result2.json

{
   "UT": "WOS:123456",
   "TI": "firstpubli",
   "DT": "journal",
   "PY": "2016",
   "TITRE" :"firstpubli2016",
   "AU": "me, you"
 }

What does it do [enter image description here][1]

Now if I put 2 publications in result2.json:

 {
   "UT": "WOS:123456",
   "TI": "firstpubli",
   "DT": "journal",
   "PY": "2016",
   "TITRE" :"firstpubli2016",
   "AU": "me, you"
 }   
 {
   "UT": "WOS:78910",
   "TI": "secondpubli",
   "DT": "journal",
   "PY": "2016",
   "TITRE" :"secondpubli2016",
   "AU": "me, you"
 }

When I run the python script to insert the data, I have this error

Traceback (most recent call last):
  File "test.py", line 45, in <module>
    filedata = json.load(fichier)
  File "/usr/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 12 column 2 - line 21 column 1 (char 143 - 282)

And nothing is inserted in the database My goal is to get like on this image, 2 productions, when I have 2 productions in my json enter image description here

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your file is not a json string. If you want more than one object to insert you should user list.
your file should be as follows :
[{ "UT": "WOS:123456", "TI": "firstpubli", "DT": "journal", "PY": "2016", "TITRE" :"firstpubli2016", "AU": "me, you" }, { "UT": "WOS:78910", "TI": "secondpubli", "DT": "journal", "PY": "2016", "TITRE" :"secondpubli2016", "AU": "me, you" }]



Also you should change your code like this :
filedata = json.load(fichier) for f in filedata: //object insertion here

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda