-
-
Notifications
You must be signed in to change notification settings - Fork 332
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
Add a filter to handle newline-delimited JSON #226
Comments
Great idea! Would it make sense to change |
Yes! That would actually be my personal preference. I realize I forgot to mention this in the issue description, but the reason I was hesitant to suggest modifying the existing
Example illustrating the two changes in behavior: package main
import (
"fmt"
"github.com/bitfield/script"
)
func main() {
data := "[0,1,2]\n[3,4,5]\n[6,7,8]"
fmt.Println(script.Echo(data).JQ(`.`).String())
fmt.Println()
fmt.Println(script.Echo(data).JQnl(`.`).String())
fmt.Println("\n---\n")
data = "[0,1,2]\ninvalid"
fmt.Println(script.Echo(data).JQ(`.`).String())
fmt.Println()
fmt.Println(script.Echo(data).JQnl(`.`).String())
} $ go run main.go
[0,1,2]
<nil>
[0,1,2]
[3,4,5]
[6,7,8]
<nil>
---
[0,1,2]
<nil>
[0,1,2]
invalid character 'i' looking for beginning of value As mentioned above, I would personally prefer to update
Let me know what you think! In either case, I would be happy to work on a PR later this week. |
Yes, this appeals to me too—presumably it's easy enough to get only the first object if that's what you really want. We are at version 0.x, so I don't mind making breaking changes if it results in a better API, which I think it will. Would you like to go ahead and make your suggested changes to |
Great! I'll work on a PR this week. re: getting only the first object, I think it will depend on the output of the JQ query/use case, e.g. in some situations the func JQSlurp(r io.Reader, w io.Writer) error {
var inputs []interface{}
dec := json.NewDecoder(r)
for dec.More() {
var v interface{}
err := dec.Decode(&v)
if err != nil {
return err
}
inputs = append(inputs, v)
}
result, err := gojq.Marshal(inputs)
if err != nil {
return err
}
_, err = fmt.Fprintln(w, string(result))
if err != nil {
return err
}
return nil
} I am not suggesting adding a However, I am open to adding one if desired. |
I would like to propose adding a new
JQnl
(orJQLines
) filter to the script package. The filter would process newline-delimited JSON data by applying a JQ query to each JSON object in the input. It should work with pretty-printed and compact (jq -c
; one JSON object per line) JSON input.Newline-delimited JSON is widely used for streaming data where each line represents a self-contained JSON object. The current
JQ
method, unlike thejq
command line utility, only processes a single JSON object from the input, making it difficult to process streams of data.Use cases
Log Analysis
Application logs are often output as JSON objects, one per line. With
JQnl
, users could extract and transform specific fields from log files, filtering for specific log levels, error messages, etc.Processing Paginated API Results
When dealing with APIs that return paginated results,
JQnl
would allow processing paginated reponses as part of a script pipeline.Data ETL Workflows
For Extract-Transform-Load workflows where each record is a separate JSON object, the method would streamline the processing of large data sets by applying transformations to each record in the stream without loading everything into memory at once or rather before applying the
JQ
filter.Concrete example for the Log Analisys use case
Extrct all warning and error messages from a JSON log file, e.g. output of
slog
./tmp/log.json
- The mixed compact-prettyprint-compact format is intentional for illustration purposes.jq
- command line utility for referenceJQ
-script.Stdin().JQ(os.Args[1]).Stdout()
JQnl
-script.Stdin().JQnl(os.Args[1]).Stdout()
Sample implementation of
JQnl
:The text was updated successfully, but these errors were encountered: