1

I have the following Python file named exampleData.json:

[
  {
    "startTime" : 1383790636.261329,
    "confidence" : 2,
    "type" : 3,
    "startTime2" : "2013-11-06 21:17:16.261",
    "duration" : 4.287207
  },
  {
    "startTime" : 1383790640.548536,
    "confidence" : 2,
    "type" : 3,
    "startTime2" : "2013-11-06 21:17:20.549",
    "duration" : 3.674701
  },
  {
    "startTime" : 1383790644.223238,
    "confidence" : 2,
    "type" : 3,
    "startTime2" : "2013-11-06 21:17:24.223",
    "duration" : 7.35066
  }
]

(The actual file has about 1000 blocks, I've just pasted 3 here). I have validated the entire JSON file using jsonlint.com, and it is valid JSON.

I am trying to run the following program named jsonGraph.py to input the code into python:

import json
import pprint

data = []
with open('D:/Users/Vik/Downloads/exampleData.json') as data_file:
    for line in data_file:
        data.append(json.loads(line))

The problem is, when I try to run the program I get the following error:

D:\Users\Name>py D:\Users\Name\Downloads\jsonGraph.py
Traceback (most recent call last):
  File "D:\Users\Name\Downloads\jsonGraph.py", line 7, in <module>
    data.append(json.loads(line))
  File "D:\Python33\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "D:\Python33\lib\json\decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Python33\lib\json\decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 2 (char 1)

Any ideas as to what is causing this error?

I have googled the error message, and there are similar posts on this website, with similar errors, but this question is unique for two reasons: 1. those were caused by using invalid JSON code, and mine is valid, and 2. It is a slightly different error message. I have also checked the python documentation and found no insights.

1
  • Technically it's not valid JSON if the top level is an array -- it has to be an object. Commented Nov 7, 2013 at 21:09

2 Answers 2

3

If the file is as you specified, you don't load it line by line, you have to load the entire file.

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

Comments

2

You have one large JSON object. Parse it in one with json.load():

import json

with open('D:/Users/Vik/Downloads/exampleData.json') as data_file:
    data = json.load(data_file)

Your code tries to treat each line as a JSON value; that won't work because [ (the first line) is not a complete JSON value, nor would combining the next few lines be.

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.