0

I have tried to remove the first key and value from a json file using python. While running the program, I came across error, they are mentioned as follows:

import json
with open('testing') as json_data:
    data = json.load(json_data)
    for element in data:
        del element['url']

Error:

Traceback (most recent call last):
  File "p.py", line 3, in <module>
    data = json.load(json_data)
  File "/usr/lib/python3.5/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 180)

The file input is something like this:

{"url":"example.com","original_url":"http://example.com","text":"blah...blah"...}
{"url":"example1.com","original_url":"http://example1.com","text":"blah...blah"...}
.
.
.
.
{"url":"exampleN.com","original_url":"http://exampleN.com","text":"blah...blah"...}

I don't know why is this problem occurring?

7
  • we'd need to see testing file, or at least start & end if it's too big. Commented Feb 8, 2017 at 10:28
  • @Jean-FrançoisFabre Yes let me edit the input Commented Feb 8, 2017 at 10:30
  • Please post lines 1-3 from the file exactly as they are. From the error message I suspect this is where the error will be found. :) Commented Feb 8, 2017 at 10:35
  • 1
    The Python json module is strict in its interpretation of JSON. The usual problem is that wherever you are getting your alleged JSON from, is not generating strictly valid JSON. A common mistake is {...}{...} or {...},{...} rather than the valid list-of-dicts [{...},{...}]. Commented Feb 8, 2017 at 10:35
  • Snap! I guessed right and you posted the confirmation just as I posted the comment! Commented Feb 8, 2017 at 10:36

3 Answers 3

2

you have to read the file line by line, since it's rather lines of json data than valid json structure

Here's my line-by-line proposal

import json
data = []
with open('testing') as f:
    for json_data in f:
       element = json.loads(json_data)  # load from current line as string
       del element['url']
       data.append(element)

Valid json would be in that case:

[{"url":"example.com","original_url":"http://example.com","text":"blah...blah"...},
{"url":"example1.com","original_url":"http://example1.com","text":"blah...blah"...}]
Sign up to request clarification or add additional context in comments.

Comments

0

As per my comment, the input file is not valid JSON.

This answer multiple json dictionaries python tells you how to successfully read such a file, which consists of a concatenation of valid JSON entities rather tyan a JSON list of such entities.

The alternative if and only if you can rely on the line-structure of the file, is to read line by line and decode each line separately.

Comments

-3

json_data is an instance of your file, not the content. so first apply read() on the instance for getting data. and second, write the full file name if you are reading a JSON file. your file should be testing.json. and third specify the mode of file opening mode. you can use this code

import json
with open('testing.json', 'r') as json_data:
    data = json.load(json_data.read())
    for element in data:
        del element['url']

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.