GeminiJSON解析Skill gemini-json-parsing

这个技能用于解析Gemini CLI的结构化输出格式,包括JSON和流JSON格式。它支持响应提取、统计解释、错误处理和工具调用分析,适用于集成工作流,特别是在程序化处理Gemini AI模型响应时。关键词:Gemini JSON 解析、CLI 输出、集成工作流、程序化处理、令牌分析、工具调用、AI应用。

AI应用 0 次安装 0 次浏览 更新于 3/11/2026

名称: gemini-json-parsing 描述: 解析Gemini CLI无头输出(JSON和流JSON格式)。涵盖响应提取、统计解释、错误处理和工具调用分析。在处理Gemini CLI程序化输出时使用。 允许工具: Read, Bash

Gemini JSON 解析

🚨 强制要求:首先调用 gemini-cli-docs

停止 - 在提供任何关于Gemini JSON输出的响应之前:

  1. 调用 gemini-cli-docs 技能
  2. 查询 特定的输出格式主题
  3. 基于 所有响应 exclusively 在官方文档加载

概述

用于解析Gemini CLI结构化输出格式的技能。对于集成工作流至关重要,特别是在Claude需要程序化处理Gemini响应时。

何时使用此技能

关键词: 解析gemini输出、json输出、流json、gemini统计、令牌使用、jq解析、gemini响应

在以下情况下使用此技能:

  • 从Gemini JSON输出中提取响应
  • 分析令牌使用和成本
  • 解析工具调用统计
  • 处理Gemini CLI错误
  • 构建自动化管道

输出格式

标准JSON (--output-format json)

完成后返回的单个JSON对象:

{
  "response": "主要的AI生成内容",
  "stats": {
    "models": {
      "gemini-2.5-pro": {
        "api": {
          "totalRequests": 2,
          "totalErrors": 0,
          "totalLatencyMs": 5053
        },
        "tokens": {
          "prompt": 24939,
          "candidates": 20,
          "total": 25113,
          "cached": 21263,
          "thoughts": 154,
          "tool": 0
        }
      }
    },
    "tools": {
      "totalCalls": 1,
      "totalSuccess": 1,
      "totalFail": 0,
      "totalDurationMs": 1881,
      "totalDecisions": {
        "accept": 0,
        "reject": 0,
        "modify": 0,
        "auto_accept": 1
      },
      "byName": {
        "google_web_search": {
          "count": 1,
          "success": 1,
          "fail": 0,
          "durationMs": 1881
        }
      }
    },
    "files": {
      "totalLinesAdded": 0,
      "totalLinesRemoved": 0
    }
  },
  "error": {
    "type": "ApiError",
    "message": "错误描述",
    "code": 500
  }
}

流JSON (--output-format stream-json)

新行分隔的JSON(JSONL)与实时事件:

事件类型 描述 字段
init 会话开始 session_id, model, timestamp
message 用户/助手消息 role, content, timestamp
tool_use 工具调用请求 tool_name, tool_id, parameters
tool_result 工具执行结果 tool_id, status, output
error 非致命错误 type, message
result 最终结果 status, stats

示例流:

{"type":"init","timestamp":"2025-10-10T12:00:00.000Z","session_id":"abc123","model":"gemini-2.5-flash"}
{"type":"message","role":"user","content":"列出文件","timestamp":"2025-10-10T12:00:01.000Z"}
{"type":"tool_use","tool_name":"Bash","tool_id":"bash-123","parameters":{"command":"ls -la"}}
{"type":"tool_result","tool_id":"bash-123","status":"success","output":"file1.txt
file2.txt"}
{"type":"message","role":"assistant","content":"以下是文件...","delta":true}
{"type":"result","status":"success","stats":{"total_tokens":250}}

常见提取模式

提取响应文本

# 获取主要响应
gemini "query" --output-format json | jq -r '.response'

# 带错误处理
result=$(gemini "query" --output-format json)
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
  echo "错误: $(echo "$result" | jq -r '.error.message')"
else
  echo "$result" | jq -r '.response'
fi

令牌统计

# 总令牌使用
echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add'

# 缓存令牌(成本节省)
echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add'

# 可计费令牌
total=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add')
cached=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add')
echo "可计费: $((total - cached))"

# 按模型令牌
echo "$result" | jq '.stats.models | to_entries[] | "\(.key): \(.value.tokens.total) 令牌"'

工具调用分析

# 总工具调用
echo "$result" | jq '.stats.tools.totalCalls'

# 列出使用工具
echo "$result" | jq -r '.stats.tools.byName | keys | join(", ")'

# 工具成功率
total=$(echo "$result" | jq '.stats.tools.totalCalls')
success=$(echo "$result" | jq '.stats.tools.totalSuccess')
echo "成功率: $((success * 100 / total))%"

# 详细工具统计
echo "$result" | jq '.stats.tools.byName | to_entries[] | "\(.key): \(.value.count) 调用, \(.value.durationMs)ms"'

模型使用

# 列出使用模型
echo "$result" | jq -r '.stats.models | keys | join(", ")'

# 模型延迟
echo "$result" | jq '.stats.models | to_entries[] | "\(.key): \(.value.api.totalLatencyMs)ms"'

# 请求计数
echo "$result" | jq '.stats.models | to_entries[] | "\(.key): \(.value.api.totalRequests) 请求"'

错误处理

# 检查错误
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
  error_type=$(echo "$result" | jq -r '.error.type // "未知"')
  error_msg=$(echo "$result" | jq -r '.error.message // "无消息"')
  error_code=$(echo "$result" | jq -r '.error.code // "N/A"')
  echo "错误 [$error_type]: $error_msg (代码: $error_code)"
  exit 1
fi

文件修改

# 行更改
echo "$result" | jq '"添加: \(.stats.files.totalLinesAdded), 移除: \(.stats.files.totalLinesRemoved)"'

流处理

按事件类型过滤

# 仅获取工具结果
gemini --output-format stream-json -p "query" | jq -r 'select(.type == "tool_result")'

# 仅获取错误
gemini --output-format stream-json -p "query" | jq -r 'select(.type == "error")'

# 获取助手消息
gemini --output-format stream-json -p "query" | jq -r 'select(.type == "message" and .role == "assistant") | .content'

实时监控

# 在发生时监视工具调用
gemini --output-format stream-json -p "analyze code" | while read line; do
  type=$(echo "$line" | jq -r '.type')
  case "$type" in
    tool_use)
      tool=$(echo "$line" | jq -r '.tool_name')
      echo "[工具] 调用: $tool"
      ;;
    tool_result)
      status=$(echo "$line" | jq -r '.status')
      echo "[结果] 状态: $status"
      ;;
    error)
      msg=$(echo "$line" | jq -r '.message')
      echo "[错误] $msg"
      ;;
  esac
done

快速参考

什么 jq 命令
响应文本 .response
总令牌 .stats.models | to_entries | map(.value.tokens.total) | add
缓存令牌 .stats.models | to_entries | map(.value.tokens.cached) | add
工具调用 .stats.tools.totalCalls
使用工具 .stats.tools.byName | keys | join(", ")
使用模型 .stats.models | keys | join(", ")
错误消息 .error.message // "none"
错误类型 .error.type // "none"
添加行 .stats.files.totalLinesAdded
移除行 .stats.files.totalLinesRemoved
总延迟 .stats.models | to_entries | map(.value.api.totalLatencyMs) | add

完整示例

#!/bin/bash
# 分析代码并报告统计

result=$(cat src/main.ts | gemini "Review this code for security issues" --output-format json)

# 检查错误
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
  echo "错误: $(echo "$result" | jq -r '.error.message')"
  exit 1
fi

# 提取响应
echo "=== 安全审查 ==="
echo "$result" | jq -r '.response'

# 报告统计
echo ""
echo "=== 统计 ==="
total=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
cached=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add // 0')
models=$(echo "$result" | jq -r '.stats.models | keys | join(", ") | if . == "" then "none" else . end')
tools=$(echo "$result" | jq '.stats.tools.totalCalls // 0')

echo "令牌: $total (缓存: $cached)"
echo "模型: $models"
echo "工具调用: $tools"

测试场景

场景1:提取响应

查询: “如何从Gemini JSON输出中提取响应?” 预期行为:

  • 技能在“解析gemini输出”或“json输出”时激活
  • 提供jq提取模式 成功标准: 用户收到.response提取命令

场景2:令牌使用分析

查询: “如何跟踪Gemini CLI的令牌使用?” 预期行为:

  • 技能在“令牌使用”或“gemini统计”时激活
  • 提供统计提取模式 成功标准: 用户收到令牌计算jq命令

场景3:流处理

查询: “如何处理Gemini CLI的流json输出?” 预期行为:

  • 技能在“流json”时激活
  • 提供JSONL处理模式 成功标准: 用户收到实时流处理示例

参考文献

查询 gemini-cli-docs 获取官方文档关于:

  • “json输出格式”
  • “流json输出”
  • “无头模式”

版本历史

  • v1.1.0 (2025-12-01): 添加测试场景部分
  • v1.0.0 (2025-11-25): 初始发布