JSON Output

TL;DR — Use --json to pipe sgrep results into jq, scripts, or other tools. Grep-compatible exit codes included.

Basic JSON

sgrep --json "parse configuration" src/

Output:

[
  {
    "path": "src/config.rs",
    "line": 42,
    "score": 0.847,
    "content": "/// Parse the TOML configuration file and return validated settings"
  },
  {
    "path": "src/cli.rs",
    "line": 18,
    "score": 0.723,
    "content": "// Load and validate CLI configuration from args + env"
  }
]

Pipeline Examples

# Open the best match in your editor
sgrep --json "authentication" src/ | jq -r '.[0].path' | xargs $EDITOR

# Count matches above threshold
sgrep --json --threshold 0.7 "memory leak" src/ | jq length

# Get just file paths (unique)
sgrep --json "error handling" src/ | jq -r '.[].path' | sort -u

# Feed into ripgrep for exact refinement
sgrep --json "database query" src/ | jq -r '.[].path' | xargs rg "SELECT"

Exit Codes

sgrep follows grep conventions:

CodeMeaning
0Matches found
1No matches above threshold
2Error (bad args, missing files, etc.)

This means you can use sgrep in shell conditionals:

if sgrep --threshold 0.8 "security vulnerability" src/ > /dev/null 2>&1; then
  echo "Potential security-related code found"
fi

JSON Schema

Each result object contains:

FieldTypeDescription
pathstringFile path relative to search root
linenumberLine number of the chunk start
scorefloatCosine similarity (0.0–1.0)
contentstringThe matched text chunk

Was this page helpful?