1

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()?

4
  • 1
    Sounds like a problem with the specific JSON file, not json.load Commented Nov 8, 2016 at 17:29
  • 1
    check if the files have the apropiarte format. You can also use try and except so only bad files don't get loaded. Commented Nov 8, 2016 at 17:30
  • @edgarcosta how would I do that in the code above? I would accept that as an answer Commented Nov 8, 2016 at 17:32
  • Sorry, @HEADLESS_ONE already answered. I hope it helps, otherwise if you still have problems write again I can try to help. Commented Nov 8, 2016 at 23:02

1 Answer 1

2

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:
    json_path = os.path.join(path_to_json, js)
    with open(json_path) as json_file:
        try:
            pd_data = json.load(json_file)
        except ValueError:
            sys.stderr.write('Could not parse JSON file: {0}'.format(json_path))

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

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

1 Comment

@data_garden, I did a small assign os.path.join(path_to_json, js) to json_path. This way, the ValueError should print in std err with the file path (not its content, as I had mistakenly done before). Hope this helps!

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.