go-jmespath icon indicating copy to clipboard operation
go-jmespath copied to clipboard

Probable bug: mysterious nulls from wildcard expression

Open phormio opened this issue 2 years ago • 0 comments

I ran this program:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/jmespath/go-jmespath"
)

func main() {
	jsonBytes := []byte(`
		{
		   "author": "Mark Twain",
		   "title": "The Adventures of Tom Sawyer",
		   "words": 80000,
		   "year": 1876
		}
	`)
	var jsonData any
	err := json.Unmarshal(jsonBytes, &jsonData)
	if err != nil {
		panic(err)
	}

	queryResult, err := jmespath.Search("*.to_string(@)", jsonData)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", queryResult)
}

Its output was:

[]interface {}{"null", "null", "null", "null", "Mark Twain", "The Adventures of Tom Sawyer", "80000", "1876"}

I don't understand where the "null" strings are coming from. This behaviour looks like a bug to me.

Here are go.mod and go.sum:

module bug-report

go 1.20

require github.com/jmespath/go-jmespath v0.4.0
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

Comparison with JavaScript

I ran these commands:

npm install [email protected]
./node_modules/jmespath/jp.js '*.to_string(@)' < book.json

Here is book.json:

{
   "author": "Mark Twain",
   "title": "The Adventures of Tom Sawyer",
   "words": 80000,
   "year": 1876
}

The output of the second command was:

["Mark Twain","The Adventures of Tom Sawyer","80000","1876"]

This is the output I expect to see from that JSON and that JMESPath query. It's obviously different from the output of the Go program.

phormio avatar Apr 07 '23 19:04 phormio