Wel, it looked incomplete to me. The bottom looked cut off, maybe also
the top. Are you sure you have the whole thing?
if it really is a sequence of commas separated JSON dicts and complete
on that basis (i.e. all the closing brackets match up etc) you need to
part it in pieces. The JSON module itself only directly supports parsing
a single JSON object, be that a dict, a list, a string or whatever -
whatever the first object is seems in the file is what you get back. But
the load() function expects to parse the whole file, hence your error
message.
There are 2 basic approaches available to you at this point, to my mind:
The first is to cut it up yourself into distinct pieces, and parse each
piece on its own. For example, you could take each dict an put it in its
own file, then parse every file. Or you could structure the single fie
you have in some way so that you can pull out each piece on its own, and
then parse that string with the json.loads() function. For example,
there’s a format called newline delimited JSON: nothing special, it just
has each JSON object on its own on a single line of text. If you edited
your file and joined up each dict to be on one line and replaced the
comma between the dicts with a line break, you’ve have this format - one
dict per line. The you read the file by reading the file a line at a
time and using json.loads() to parse each line on its own. Think of it a
bit like CSV, but using JSON.
The next is, if the file really is a valid comma separated sequence of
JSON dicts, is just to make that a single array. So, your file looks
like:
{ ...
...
...
},{...
...
...},{...
...
...}
Put it in brackets:
[
{ ...
...
...
},{...
...
...},{...
...
...}
]
by just inserting an opening and closing square bracket at the top and
bottom. Now it is a single JSON object, a list, which contains all the
dicts. json.load() should be happy.
Cheers,
Cameron Simpson cs@cskk.id.au