Scrungo
Scrungo

Reputation: 25

Python - JSONDecodeError: Expecting property name enclosed in double quotes

I've checked out the existing answers for this question and I can't find what my specific issue is. I'm trying to parse what I think is a valid JSON object, but somehow it's rejecting it.

I used this code to dump the data to the file from an API response:

def write_video_ids_to_file(video_ids, video_data_file):
    # Create file if it doesn't exist
    file_is_empty = is_file_empty(video_data_file)
    if file_is_empty:
        open(video_data_file, 'w').close()

    with open(video_data_file, 'a') as file:
        json.dump(video_ids, file, indent=4, sort_keys=True, default=str)
        print('collecting video ids')

And this code below that's running to try to load that JSON back out of the file seems to be failing:

def construct_video_id_array(video_data_file):
    with open(video_data_file, 'r') as file:
        json_data = json.load(file) # <-- Failing here

    video_ids_separate = []
    for video in json_data:
        video_ids_separate.append(video['video_id'])

    return video_ids_separate

Here's a sample of my JSON data (there's like 8 MB of it, so I'm not going to post it all):

{[
    {
        "channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
        "collection_date": "2020-08-18 20:55:18.854967",
        "publish_date": 1597784402.0,
        "video_id": "5tBnaxQKpHQ"
    },
    {
        "channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
        "collection_date": "2020-08-18 20:55:18.854967",
        "publish_date": 1597698002.0,
        "video_id": "7J3H8ckUaYU"
    }
]}

I originally didn't have the { at the beginning and end, so I added those. I am not sure if that was the right move, but for what it's worth, it's not working with or without it, just throwing different errors.

Upvotes: 2

Views: 19911

Answers (1)

Insane Miner
Insane Miner

Reputation: 140

Your JSON content should look like:

{ "your_data":[
    {
        "channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
        "collection_date": "2020-08-18 20:55:18.854967",
        "publish_date": 1597784402.0,
        "video_id": "5tBnaxQKpHQ"
    },
    {
        "channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
        "collection_date": "2020-08-18 20:55:18.854967",
        "publish_date": 1597698002.0,
        "video_id": "7J3H8ckUaYU"
    }
]}

This is valid. Check this website out: https://www.geeksforgeeks.org/json-load-in-python/ and also https://jsonlint.com/.

All JSON data will need a key, even a list.

Your JSON object should also look like:

{ "your_data":[
    {
        "channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
        "collection_date": "2020-08-18 20:55:18.854967",
        "publish_date": 1597784402.0,
        "video_id": "5tBnaxQKpHQ"
    },
    {
        "channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
        "collection_date": "2020-08-18 20:55:18.854967",
        "publish_date": 1597698002.0,
        "video_id": "7J3H8ckUaYU"
    }
]}

So file json.dump will dump the proper JSON content in a file.

Upvotes: 1

Related Questions