data_garden data_garden - 1 month ago 34
Python Question

Python - JSON ValueError: Extra data

I am trying to join

json
files:

path_to_json = 'generated_playlists/p1/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]


json
files are structured like this:

{"user1": {"Wild Wood": 1.0, "You Do Something To Me": 1.0, "Reprise": 1.0}}


but when I do:

for js in json_files:
with open(os.path.join(path_to_json, js)) as json_file:
pd_data = json.load(json_file)


I get:

ValueError: Extra data: line 1 column 145 - line 1 column 721 (char 144 - 720)


what is wrong with
json.load()
?

Answer

As suggested by @edgarcosta, you can handle the ValueError in a try-except block in your for loop, like so:

import os
import json
import sys

path_to_json = 'generated_playlists/p1/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

for js in json_files:
    with open(os.path.join(path_to_json, js)) as json_file:
        try:
            pd_data = json.load(json_file)
        except ValueError:
            sys.stderr.write('Could not parse JSON file: {0}'.format(json_file))

This should help you identify which JSON files are unable to be read.