0

I have data, which look like [["header","row"],["5","16"], ...] In case they are saved in file, they can be easily read by

with open(input_data, 'r') as f:
     data = json.load(f)

It should be possible to read them into data straightforwardly but somehow the input string can't be converted to json data = json.loads(x) returns ValueError: No JSON object could be decoded

What am I missing?

4
  • f.read() vs. just fjson.load(f.read()). You're currently trying to load the File Object itself rather than the string of JSON from the file on disk. Commented Nov 29, 2015 at 5:58
  • You also need to make sure your data abides by the json format. Commented Nov 29, 2015 at 6:07
  • json.load(x) is correct if x is an file object. json.loads(x) should be used if x is a string. --- Your code example shows load but your error shows loads; which one is it? Commented Nov 29, 2015 at 6:21
  • the version with the file works but ones with the string - doesn't Commented Nov 29, 2015 at 6:27

1 Answer 1

1

The error ValueError tells you that the JSON is not valid. Correct the JSON in the file or the string and it will load properly.

Your code will correctly load in JSON data from a file if input_data is a file name.


json.load() vs json.loads()

Both of these functions will process json.

json.load() takes a file like object.

json.loads() takes a string or unicode object.


JSON file:

[["header","row"],["5","16"]]

This code will correctly read the above JSON from a file:

input_data = 'json_file_name.json'
with open(input_data, 'r') as f:
    data = json.load(f)
print(data)

This will process the JSON stored in a string:

json_string = '[["header","row"],["5","16"]]'
print(json.loads(json_string))
Sign up to request clarification or add additional context in comments.

5 Comments

I don't have a problem with the file. I do not want to use the file. The content is recognized being json when it is in a file. It is not recognized as such if it's just input string. I don't understand why.
As indicated in my answer, if the JSON content is in a string you must use json.loads() with the trailing s and not json.load(). If you feel that your string content is well formatted, please update your question to include the exact python code with the string containing the JSON. Possibly there is a formatting issue.
I do use json.loads() for the string and get error. The string imho isn't very well formatted json but being in a file, json.load() is able to handle it
I agree that it is quite strange that it would read it from the file correctly but not from a string. The only thing I can think of is a strange formatting issue but the json processor should be ignoring whitespace. If you post the string version of the JSON in your question I'd be happy to review it. You say it isn't well formatted, do you have the permissions to update the file?
it looks like the string had missing quotes (it comes as api result, which is then sent to processing script). Now I'm getting something reasonable. Thanks

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.