Home >Backend Development >Python Tutorial >How to Parse a JSON Lines File in Python and Avoid 'ValueError: Extra data'?
Loading and Parsing a JSON File with Multiple JSON Objects
Problem:
Attempting to load a JSON file in Python yields a "ValueError: Extra data" error. Despite consulting the Python documentation, the solution remains elusive.
Solution:
The given JSON file is not in a single JSON object format; it's a JSON Lines format file. This means each line contains a valid JSON object, without a top-level list or object definition. To parse this type of file:
import json data = [] with open('file') as f: for line in f: data.append(json.loads(line))
By iterating line by line and parsing each line individually, the memory consumption is minimized.
Note:
If the JSON file contains individual objects separated by delimiters, refer to the resource "How do I use the 'json' module to read in one JSON object at a time?" for parsing individual objects using a buffered method.
The above is the detailed content of How to Parse a JSON Lines File in Python and Avoid 'ValueError: Extra data'?. For more information, please follow other related articles on the PHP Chinese website!