Skip to content
Başlangıç Orta

Yanıtları Akışla Alma (NDJSON)

Kullanıcı arayüzleri ve pipeline'lar oluşturmak için gerçek zamanlı olayları NDJSON olarak alın

Komut

$ "color:#7C5CFC">claude -p "Explain recursion" "color:#d97757">--output-format stream-json "color:#d97757">--verbose

Yanıt

{"type": "system","subtype": "init","tools":[...],"model": "claude-opus-4-6"}
{"type": "assistant","message":{"content":[...]}}
{"type": "rate_limit_event","rate_limit_info":{...}}
{"type": "result","subtype": "success","total_cost_usd": 0.08}

Ayrıştırma Kodu

059669">">// Parse NDJSON line by line — NOT as a JSON array
059669">">const lines = output.trim().split(059669059669">">'\n');
059669">">for (059669">">const line 059669">">of lines) {
  059669">">const event = JSON.parse(line);
  059669">">if (event.type === 059669059669">">'result') console.log(event.result);
}

Tuzaklar

! init olayını almak için stream-json ile birlikte --verbose gereklidir
! NDJSON = satır başına bir JSON. Satır satır ayrıştırın, tüm çıktıyı JSON.parse ile asla ayrıştırmayın

İlgili Tarifler