#400·gjson

`Result.Path` panics with index out of range on malformed JSON

Author: DRMacIverCreated Sep 23, 2026Updated Sep 29, 2026

On a document with no colon between a key and its value, Get reads the value without complaint, but calling Path on that result panics:

go
package main

import (
	"fmt"

	"github.com/tidwall/gjson"
)

func main() {
	doc := `{"b:"2}`
	r := gjson.Get(doc, "b:")
	fmt.Println("Raw:", r.Raw)
	fmt.Println("Path:", r.Path(doc))
}

Output:

Raw: 2
panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/tidwall/gjson.revSquash(...)
	gjson.go:3378 +0x198
github.com/tidwall/gjson.Result.Path(...)
	gjson.go:3491 +0x57c
main.main()
	main.go:13 +0xf0

The README says that bad JSON will not panic but may return unexpected results. Get behaves that way here (it returns 2 as the value of "b:"); Path on the same result does not.

Tested on gjson v1.19.0 and on current master (8d89927).

BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 6 other bugs in gjson. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/gjson. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.