Skip to content
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

Allow processing of a stream of separate JSON elements #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 jp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

Expand Down Expand Up @@ -33,6 +34,10 @@ func main() {
Usage: "If the final result is a string, it will be printed without quotes.",
EnvVar: "JP_UNQUOTED",
},
cli.BoolFlag{
Name: "stream, s",
Usage: "Parse JSON elements until the input stream is exhausted (rather than just the first).",
},
cli.BoolFlag{
Name: "ast",
Usage: "Only print the AST of the parsed expression. Do not rely on this output, only useful for debugging purposes.",
Expand Down Expand Up @@ -82,7 +87,6 @@ func runMain(c *cli.Context) int {
fmt.Printf("%s\n", parsed)
return 0
}
var input interface{}
var jsonParser *json.Decoder
if c.String("filename") != "" {
f, err := os.Open(c.String("filename"))
Expand All @@ -94,30 +98,38 @@ func runMain(c *cli.Context) int {
} else {
jsonParser = json.NewDecoder(os.Stdin)
}
if err := jsonParser.Decode(&input); err != nil {
errMsg("Error parsing input json: %s\n", err)
return 2
}
result, err := jmespath.Search(expression, input)
if err != nil {
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
return errMsg("%s\n%s\n",
syntaxError,
syntaxError.HighlightLocation())
for {
var input interface{}
if err := jsonParser.Decode(&input); err == io.EOF {
break
} else if err != nil {
errMsg("Error parsing input json: %s\n", err)
return 2
}
return errMsg("Error evaluating JMESPath expression: %s", err)
}
converted, isString := result.(string)
if c.Bool("unquoted") && isString {
os.Stdout.WriteString(converted)
} else {
toJSON, err := json.MarshalIndent(result, "", " ")
result, err := jmespath.Search(expression, input)
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
return errMsg("%s\n%s\n",
syntaxError,
syntaxError.HighlightLocation())
}
return errMsg("Error evaluating JMESPath expression: %s", err)
}
converted, isString := result.(string)
if c.Bool("unquoted") && isString {
os.Stdout.WriteString(converted)
} else {
toJSON, err := json.MarshalIndent(result, "", " ")
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
}
os.Stdout.Write(toJSON)
}
os.Stdout.WriteString("\n")
if !c.Bool("stream") {
break
}
os.Stdout.Write(toJSON)
}
os.Stdout.WriteString("\n")
return 0
}
11 changes: 11 additions & 0 deletions test/cases/search.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
[ "$output" == "\"bar\"" ]
}

@test "Ignores subsequent data" {
output=$(echo '{"foo": "bar"}blah' | ./jp foo)
[ "$output" == "\"bar\"" ]
}

@test "Processes subsequent data in stream mode" {
output=$(echo '{"foo": "bar"}{"foo": "x"}' | ./jp -s foo)
echo "$output"
[ "$output" == $'\"bar\"\n\"x\"' ]
}

@test "Can search subexpr expression" {
output=$(echo '{"foo": {"bar": "baz"}}' | ./jp foo.bar)
[ "$output" == "\"baz\"" ]
Expand Down