3

I have a json file. A simplified version of it looks as following:

{
  "host": "a.com",
  "ip": "1.2.2.3",
  "port": 8
}
{
  "host": "b.com",
  "ip": "2.5.0.4",
  "port": 3

}
{
  "host": "c.com",
  "ip": "9.17.6.7",
  "port": 4
}

I run this script parser.py to parse it:

import json
from pprint import pprint

with open('myfile.json') as f:
    data = json.load(f)
pprint(data)

But I'm getting this error:

Traceback (most recent call last):
  File "parser.py", line 5, in <module>
    data = json.load(f)
  File "/usr/lib/python3.6/json/__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 6 column 1 (char 54)

Can you please point to me what's missing?

user9371654
  • 2,160
  • 16
  • 45
  • 78
  • 4
    Your JSON file contains multiple top-level objects, which means it is not valid JSON. Perhaps you meant to wrap them in an array? – jwodder Aug 19 '18 at 16:31
  • @jwodder thanks. Can you please clarify how can I wrap them? it is a huge file. – user9371654 Aug 19 '18 at 16:33

4 Answers4

21

As you already found out: that is not valid JSON.
You have to modify it to make it valid, specifically, you have to wrap your top-level objects in an array. Try this:

import json
from pprint import pprint

with open('myfile.json') as f:
    data = json.loads("[" + 
        f.read().replace("}\n{", "},\n{") + 
    "]")

    print(data)
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
2

Your JSON data set is not valid , You can merge them into one array of objects. For example :

[
    {
        "host": "a.com",
        "ip": "1.2.2.3",
        "port": 8
    }, {
        "host": "b.com",
        "ip": "2.5.0.4",
        "port": 3

    }, {
        "host": "c.com",
        "ip": "9.17.6.7",
        "port": 4
    }
]

In JSON you can't have multiple objects of top-level but you can have array of objects and it is valid

You can see more JSON Data Set examples if you want in this link

M4HdYaR
  • 1,124
  • 11
  • 27
  • 1
    in my case I cannot just open the 2GB file with text editor just to add a pair of brackets – GGEv Jun 19 '19 at 14:21
0

Your JSON file isn't available in proper format.
It should be like this, inside a list, comma separated:

[
    {
     "host": "a.com",
     "ip": "1.2.2.3",
     "port": 8
    },
    {
     "host": "b.com",
     "ip": "2.5.0.4",
     "port": 3

    },
    {
     "host": "c.com",
     "ip": "9.17.6.7",
     "port": 4
    },
]
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
wahsandaruwan
  • 587
  • 5
  • 7
-1

Here, in this case, you can convert the JSON in the array format and this will work for you. Also to separate the data the comma (,) is missing in the JSON. Please fix that and the code will work