0

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

1
  • Shouldn't you put comma between entries in JSON? I mean, } , { and so on? Commented Apr 26, 2017 at 9:51

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.