Skip to content
Advertisement

How to fix the Python and JSON Delimiter Expected Error?

I’m entering the following code and receiving the delimiter expected error. From what I can gather JSON Decoder wants a ‘,’ automatically inserted between each record I pull from the For Loop. I can’t seem to find any solution. It pulls one record from the URL, then stops.
Line 1, Column 54 (char 53) does not appear to exist. There are 9 columns on line 1.

import pandas as pd
import requests
import json
import urllib.request


sample = pd.read_csv('Codes.csv')

with open('FilmList2.json', 'r+') as filmlist:
    data = json.load(filmlist)


def compare():
    for code in sample:
        code = urllib.parse.quote(code)
        with urllib.request.urlopen(f'http://www.omdbapi.com/?i={code}&type=movie&apikey=71c66a81') as url:
            entry = json.load(url)
            data.update(entry)
            with open('FilmList2.json', 'a') as filmlist:
                json.dump(data, filmlist, indent=4, separators = (', ',': '))       

compare()

Error Message Missing Delimiter

Advertisement

Answer

Yeah, JSON format delimit each element with an , so if your file is not properly formated, the json package will not works.

This does not works so it will raise an error:

{
  "name": "paul"
  "age": 18
}

But this works fine (because it is properly formatted):

{
  "name": "paul",
  "age": 18
}

So, you just should gather the data from the file, store it in a variable, update the variable with the new data (it’s what your doing).

But then, don’t append the content to the file, re-write it with the new value (on line 19, use "w" instead of "a":

with open('FilmList2.json', 'a') as filmlist:
    json.dump(data, filmlist, indent=4, separators = (', ',': '))
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement