wavetermdev/waveterm · error

invalid index at position %d: %v

Error message

invalid index at position %d: %v

What it means

Inside bracket syntax, ParseSimplePath requires the contents between '[' and ']' to parse as an integer via strconv.Atoi. If it does not (e.g. 'abc', '', '1.5'), it returns 'invalid index at position %d' with the underlying Atoi error. Array indices in ijson paths must be plain non-negative integers.

Source

Thrown at pkg/ijson/ijson.go:74

		"data": value,
	}
}

var pathPartKeyRe = regexp.MustCompile(`^[a-zA-Z0-9:_#-]+`)

func ParseSimplePath(input string) ([]any, error) {
	var path []any
	// Scan the input string character by character
	for i := 0; i < len(input); {
		if input[i] == '[' {
			// Handle the index
			end := strings.Index(input[i:], "]")
			if end == -1 {
				return nil, fmt.Errorf("unmatched bracket at position %d", i)
			}
			index, err := strconv.Atoi(input[i+1 : i+end])
			if err != nil {
				return nil, fmt.Errorf("invalid index at position %d: %v", i, err)
			}
			path = append(path, index)
			i += end + 1
		} else {
			// Handle the key
			j := i
			for j < len(input) && input[j] != '.' && input[j] != '[' {
				j++
			}
			key := input[i:j]
			if !pathPartKeyRe.MatchString(key) {
				return nil, fmt.Errorf("invalid key at position %d: %s", i, key)
			}
			path = append(path, key)
			i = j
		}
		if i < len(input) && input[i] == '.' {
			i++

View on GitHub (pinned to a4447c1563)

Solutions

  1. Use dot notation for keys: "items.name" instead of "items[name]".
  2. Ensure bracket contents are a plain integer: "items[0]".
  3. Sanitize user-supplied path strings with a regex like ^\w+(\.\w+|\[\d+\])*$ before parsing.

Example fix

// before
path, err := ijson.ParseSimplePath("items[first]") // invalid index
// after
path, err := ijson.ParseSimplePath("items.0")        // key form, or:
_ = path
path2, err2 := ijson.ParseSimplePath("items[0]")     // integer index form
Defensive patterns

Strategy: validation

Validate before calling

var simplePathRe = regexp.MustCompile(`^[a-zA-Z0-9:_#-]+(\.[a-zA-Z0-9:_#-]+|\[\d+\])*$`)
if !simplePathRe.MatchString(userPath) {
    return fmt.Errorf("path must be key.name[0] form with integer indices")
}

Try / catch

path, err := ijson.ParseSimplePath(input)
if err != nil {
    if strings.Contains(err.Error(), "invalid index") {
        return fmt.Errorf("array indices must be integers, e.g. items[0]: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling ParseSimplePath with bracket contents that are not integers: "items[first]", "a[]", "list[01x]", or a negative-looking/fractional index.

Common situations: Users writing named keys inside brackets (JavaScript-object habit) instead of dot notation; empty brackets from templating; copy-pasting paths from JS where bracket contents were strings.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/3c9c26ffcd27a35c. Report an issue: GitHub.