Skip to content
Getting Started Simple

Parse JSON Output

Get the full metadata envelope with cost, tokens, and session ID

Command

$ "color:#7C5CFC">claude -p "Say exactly: Hello CLI Test" "color:#d97757">--output-format json

Response

{
  "type": "result",
  "subtype": "success",
  "result": "Hello CLI Test",
  "session_id": "0e639165-79b1-4bd4-bb0f-4cb4c15115d4",
  "total_cost_usd": 0.008,
  "usage": { "input_tokens": 3, "output_tokens": 6 }
}

Parsing Code

059669">">const data = JSON.parse(output);
059669">">if (data.is_error) 059669">">throw 059669">">new Error(data.result);
059669">">console.log(data.result);           059669">">// "Hello CLI Test059669">"
059669">">console.log(data.total_cost_usd);   059669">">// 0.008
059669">">console.log(data.session_id);       059669">">// UUID for --resume

Gotchas

! result field contains the text response (same as --output-format text)
! session_id is needed for --resume to continue the conversation later

Related Recipes