0

I have a JSON file named debug.json that I created in Python3.3 that looks like this:

{"TIME": 55.55, "ID":155,"DATA": [17,22,33,44,55]}{"TIME": 56.55, "ID":195,"DATA": [17,22,ff,44,55]}

I'm trying to load it with the following code:

import json
with open("debug.json",'r',encoding='utf-8') as f:
    testing = json.loads(f.read())

However when I try this I get the following error:

ValueError: Extra data line 1 column 92

This is where the second JSON object starts in the text file...I'm guessing that I am missing something pretty trivial here but I haven't found any examples that relate to my problem. Any help is appreciated!

1
  • I know this question is quite old, but FYI for anyone else who stumbles across it: The real issue here is that a JSON document - which is what python's json parser knows how to read - is composed of exactly one JSON value, which can be an object, array, string, number, boolean, or null. The contents of "debug.json" is not a valid JSON document; it's multiple JSON objects in a row. That's why it needs to be read piecemeal with raw_decode(). Commented May 31, 2022 at 2:42

2 Answers 2

2

Use json.JSONDecoder.raw_decode, which accepts JSON with extra data at the end, such as another JSON object, and returns a tuple with the first object decoded and the position of the next object.

Example with your JSON :

import json

js = """{"TIME": 55.55, "ID":155,"DATA": [17,22,33,44,55]}{"TIME": 56.55, "ID":195,"DATA": [17,22,ff,44,55]}"""

json.JSONDecoder().raw_decode(js) # ({'TIME': 55.55, 'DATA': [17, 22, 33, 44, 55], 'ID': 155}, 50)

js[50:] # '{"TIME": 56.55, "ID":195,"DATA": [17,22,ff,44,55]}'

As you can see, it successfully decoded the first object and told us where the next object starts (in this case at the 50th character).

Here is a function that I made that can decode multiple JSON objects and returns a list with all of them :

def multipleJSONDecode(js):
    result = []
    while js:
        obj, pos = json.JSONDecoder().raw_decode(js)
        result.append(obj)
        js = js[pos:]
    return result
Sign up to request clarification or add additional context in comments.

Comments

0

When you create the file, make sure that you have at most one valid JSON string per line. Then, when you need to read them back out, you can loop over the lines in the file one at a time:

import json
testing = []
with open("debug.json",'r',encoding='utf-8') as f:
    for line in f:
        testing.append(json.loads(line))

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.