When attempting to load a JSON file containing multiple JSON objects separated by newline characters (JSON lines format), you may encounter the following error:
ValueError: Extra data: line 2 column 1 - line 225116 column 1 (char 232 - 160128774)
This error occurs because, while each individual line is valid JSON, the file itself is not a valid JSON value as there is no top-level list or object definition.
To parse a JSON lines file, use the following approach:
import json data = [] with open('file') as f: for line in f: data.append(json.loads(line))
In this example, the following steps are performed:
Parsing JSON lines offers several benefits:
If your file contains individual JSON objects delimited by newline characters, you can use the method described in "How do I use the 'json' module to read in one JSON object at a time?" to parse out individual objects using a buffered method.
The above is the detailed content of How Do I Parse Multiple JSON Objects from a JSON Lines File in Python?. For more information, please follow other related articles on the PHP Chinese website!