
How to Process Large JSON Files in Python With ijson
I loaded a large JSON file in Python and watched memory jump from 43 MB to 808 MB. Here’s what happened and how streaming changed everything.
以下正文同步自 KDnuggets,版权归原站所有,已转换为易读排版。
JSON is one of the most common formats for storing and exchanging data in Python applications. For small files, Python's built-in json module works perfectly well:
import json
with open("data.json") as f: data = json.load(f)
The problem starts when the file becomes large.
json.load() parses the entire JSON document and creates the corresponding Python objects in memory before you can start processing the data. A file that occupies a few hundred megabytes on disk can therefore require much more memory once converted into Python dictionaries, lists, strings, and numbers.
A better option for this type of workload is ijson, an iterative JSON parser that lets you process data incrementally. In this tutorial, we will use a large JSON file containing San Francisco land-parcel data. We will first process it with json.load(), measure what happens to memory, and then solve the same task with ijson.
For this experiment, we will use the San Francisco City Lots dataset. I am using Google Colab for this article. Download it using:
curl -L
https://raw.githubusercontent.com/zemirco/sf-city-lots-json/master/citylots.json
-o citylots.json
You can check its size with:
ls -lh citylots.json
The dataset is 181 MB in size, making it large enough to demonstrate the difference between loading and streaming JSON. The file follows the standard GeoJSON FeatureCollection structure. You can check it using this code:
import json
with open("citylots.json", "r") as f: data = json.load(f)
print(data.keys()) data["features"][0]
Output:
dict_keys(['type', 'features'])
{'type': 'Feature', 'properties': {'MAPBLKLOT': '0001001', 'BLKLOT': '0001001', 'BLOCK_NUM': '0001', 'LOT_NUM': '001', 'FROM_ST': '0', 'TO_ST': '0', 'STREET': 'UNKNOWN', 'ST_TYPE': None, 'ODD_EVEN': 'E'}, 'geometry': {'type': 'Polygon', 'coordinates': [[[-122.42200352825247, 37.80848009696725, 0.0], [-122.42207601332528, 37.808835019815085, 0.0], [-122.42110217434863, 37.808803534992904, 0.0], [-122.42106256906727, 37.80860105681815, 0.0], [-122.42200352825247, 37.80848009696725, 0.0]]]}}
The important part for us is the features array.
Each feature represents one parcel and contains two main sections:
feature ├── properties │ ├── BLKLOT │ ├── BLOCK_NUM │ ├── LOT_NUM │ └── STREET │ └── geometry └── coordinates
Our analysis only needs a few values under properties. However, if we use the standard JSON parser, Python still has to load all of the geometry coordinates as well.
Let's first use the approach most Python developers would naturally try. Create a file called standard_json.py:
import json import time
start = time.perf_counter()
with open("citylots.json", "r") as f: data = json.load(f)
features = data["features"]
market_count = 0 first_five = []
for feature in features:
properties = feature["properties"]
if properties.get("STREET") == "MARKET":
market_count += 1
if len(first_five)
The code is simple and perfectly valid.
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。