0

I am trying to load a valid json file (according to jsonlint.com) into python to then index it into elasticsearch.

MY CODE:

file = open(json_file)
i=1
json_data = [json.loads(line) for line in file]
print(json_data)

ERROR I AM RECEIVING:

json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)

I have checked other's solutions which focus on the invalidity of the json file but mine is completely valid.

SHORTENED VERSION OF MY JSON FILE:

[
  {"sepalLength": 5.1, "sepalWidth": 3.5, "petalLength": 1.4, "petalWidth": 0.2, "species": "setosa"},
  {"sepalLength": 4.9, "sepalWidth": 3.0, "petalLength": 1.4, "petalWidth": 0.2, "species": "setosa"},
  {"sepalLength": 4.7, "sepalWidth": 3.2, "petalLength": 1.3, "petalWidth": 0.2, "species": "setosa"},
  {"sepalLength": 4.6, "sepalWidth": 3.1, "petalLength": 1.5, "petalWidth": 0.2, "species": "setosa"}
]

I have no idea what seems to be the problem here.

1 Answer 1

3

Your list comprehension is trying to load as many objects as there are lines in your file, but there is just one file. Use this:

import json

with open(json_file, 'r') as f:
    json_data = [line for line in json.load(f)]
    print(json_data)
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.