#768·ast-grep

[feature] Support ripgrep JSON format

Author: dandavisonCreated Dec 10, 2023Updated Aug 16, 2026
Labelsenhancement

⭐ Suggestion

Hi @HerringtonDarkholme, ast-grep looks great. What do you think about adding support for ripgrep's JSON output format, so that tools that have been written to handle ripgrep's format would automatically work with ast-grep. (I'm the author of delta, which can render ripgrep's JSON format; see https://github.com/BurntSushi/ripgrep#related-tools:

image

).

In addition to the fact that ripgrep is popular and tooling has been written that targets its JSON format it's possible ripgrep's JSON format has other advantages, for example, rg --json retains non-matching context lines when -A, -B, or -C are used, whereas I believe ast-grep --json=stream doesn't.

Perhaps ast-grep --json=ripgrep could be identical to rg --json at the intersection of their output, but could also contain additional keys (e.g. language).

$ cat b.rs
aaa
bbb bbb bbb
ccc
$  sg --json=stream -B1 -p bbb .
{"text":"bbb","range":{"byteOffset":{"start":4,"end":7},"start":{"line":1,"column":0},"end":{"line":1,"column":3}},"file":"./b.rs","lines":"bbb bbb bbb","language":"Rust"}
{"text":"bbb","range":{"byteOffset":{"start":8,"end":11},"start":{"line":1,"column":4},"end":{"line":1,"column":7}},"file":"./b.rs","lines":"bbb bbb bbb","language":"Rust"}
{"text":"bbb","range":{"byteOffset":{"start":12,"end":15},"start":{"line":1,"column":8},"end":{"line":1,"column":11}},"file":"./b.rs","lines":"bbb bbb bbb","language":"Rust"}
$ rg -B1 --json bbb b.rs
{"type":"begin","data":{"path":{"text":"b.rs"}}}
{"type":"context","data":{"path":{"text":"b.rs"},"lines":{"text":"aaa\n"},"line_number":1,"absolute_offset":0,"submatches":[]}}
{"type":"match","data":{"path":{"text":"b.rs"},"lines":{"text":"bbb bbb bbb\n"},"line_number":2,"absolute_offset":4,"submatches":[{"match":{"text":"bbb"},"start":0,"end":3},{"match":{"text":"bbb"},"start":4,"end":7},{"match":{"text":"bbb"},"start":8,"end":11}]}}
{"type":"end","data":{"path":{"text":"b.rs"},"binary_offset":null,"stats":{"elapsed":{"secs":0,"nanos":160166,"human":"0.000160s"},"searches":1,"searches_with_match":1,"bytes_searched":20,"bytes_printed":440,"matched_lines":1,"matches":3}}}
{"data":{"elapsed_total":{"human":"0.001159s","nanos":1159416,"secs":0},"stats":{"bytes_printed":440,"bytes_searched":20,"elapsed":{"human":"0.000160s","nanos":160166,"secs":0},"matched_lines":1,"matches":3,"searches":1,"searches_with_match":1}},"type":"summary"}

Use Cases

  • What do you want to use this for? Processing ast-grep output with existing tools
  • What workarounds are you using in the meantime? People could write scripts to transform ast-grep into ripgrep format (have to coalesce successive matches into a single record), but it wouldn't be very convenient/portable, and wouldn't get around the problem that context lines are missing.