Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

350
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!