Skip to content

Change JQ to process newline-delimited JSON #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,38 +712,50 @@ func (p *Pipe) Join() *Pipe {
})
}

// JQ executes query on the pipe's contents (presumed to be JSON), producing
// the result. An invalid query will set the appropriate error on the pipe.
// JQ executes query on the pipe's contents (presumed to be valid JSON or
// [JSONLines] data), applying the query to each newline-delimited input value
// and producing results until the first error is encountered. An invalid query
// or value will set the appropriate error on the pipe.
//
// The exact dialect of JQ supported is that provided by
// [github.com/itchyny/gojq], whose documentation explains the differences
// between it and standard JQ.
//
// [JSONLines]: https://jsonlines.org/
func (p *Pipe) JQ(query string) *Pipe {
parsedQuery, err := gojq.Parse(query)
if err != nil {
return p.WithError(err)
}
code, err := gojq.Compile(parsedQuery)
if err != nil {
return p.WithError(err)
}
return p.Filter(func(r io.Reader, w io.Writer) error {
q, err := gojq.Parse(query)
if err != nil {
return err
}
var input interface{}
err = json.NewDecoder(r).Decode(&input)
if err != nil {
return err
}
iter := q.Run(input)
for {
v, ok := iter.Next()
if !ok {
return nil
}
if err, ok := v.(error); ok {
return err
}
result, err := gojq.Marshal(v)
dec := json.NewDecoder(r)
for dec.More() {
var input any
err := dec.Decode(&input)
if err != nil {
return err
}
fmt.Fprintln(w, string(result))
iter := code.Run(input)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return err
}
result, err := gojq.Marshal(v)
if err != nil {
return err
}
fmt.Fprintln(w, string(result))
}
}
return nil
})
}

Expand Down
53 changes: 48 additions & 5 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,6 @@ func TestJQWithDotQueryPrettyPrintsInput(t *testing.T) {
t.Fatal(err)
}
if want != got {
t.Error(want, got)
t.Error(cmp.Diff(want, got))
}
}
Expand All @@ -757,7 +756,6 @@ func TestJQWithFieldQueryProducesSelectedField(t *testing.T) {
t.Fatal(err)
}
if want != got {
t.Error(want, got)
t.Error(cmp.Diff(want, got))
}
}
Expand All @@ -771,7 +769,6 @@ func TestJQWithArrayQueryProducesRequiredArray(t *testing.T) {
t.Fatal(err)
}
if want != got {
t.Error(want, got)
t.Error(cmp.Diff(want, got))
}
}
Expand All @@ -785,7 +782,6 @@ func TestJQWithArrayInputAndElementQueryProducesSelectedElement(t *testing.T) {
t.Fatal(err)
}
if want != got {
t.Error(want, got)
t.Error(cmp.Diff(want, got))
}
}
Expand All @@ -799,7 +795,32 @@ func TestJQHandlesGithubJSONWithRealWorldExampleQuery(t *testing.T) {
t.Fatal(err)
}
if want != got {
t.Error(want, got)
t.Error(cmp.Diff(want, got))
}
}

func TestJQCorrectlyQueriesMultilineInputFields(t *testing.T) {
t.Parallel()
input := `{"a":1}` + "\n" + `{"a":2}`
want := "1\n2\n"
got, err := script.Echo(input).JQ(".a").String()
if err != nil {
t.Fatal(err)
}
if want != got {
t.Error(cmp.Diff(want, got))
}
}

func TestJQCorrectlyQueriesMultilineInputArrays(t *testing.T) {
t.Parallel()
input := `[1, 2, 3]` + "\n" + `[4, 5, 6]`
want := "1\n4\n"
got, err := script.Echo(input).JQ(".[0]").String()
if err != nil {
t.Fatal(err)
}
if want != got {
t.Error(cmp.Diff(want, got))
}
}
Expand All @@ -813,6 +834,28 @@ func TestJQErrorsWithInvalidQuery(t *testing.T) {
}
}

func TestJQErrorsWithInvalidInput(t *testing.T) {
t.Parallel()
input := "invalid JSON value"
_, err := script.Echo(input).JQ(".").String()
if err == nil {
t.Error("want error from invalid JSON input, got nil")
}
}

func TestJQProducesValidResultsUntilFirstError(t *testing.T) {
t.Parallel()
input := "[1]\ninvalid JSON value"
want := "1\n"
got, err := script.Echo(input).JQ(".[0]").String()
if err == nil {
t.Fatal("want error from invalid JSON input, got nil")
}
if want != got {
t.Error(cmp.Diff(want, got))
}
}

func TestLastDropsAllButLastNLinesOfInput(t *testing.T) {
t.Parallel()
input := "a\nb\nc\n"
Expand Down