Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

348
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda