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

EachKey cannot extract from array of strings #232

Open
leodido opened this issue Jul 19, 2021 · 1 comment
Open

EachKey cannot extract from array of strings #232

leodido opened this issue Jul 19, 2021 · 1 comment

Comments

@leodido
Copy link

leodido commented Jul 19, 2021

The EachKey API does not work coherently to the Get one w.r.t array of strings.

Meaning that it does not work on paths like "request>headers>User-Agent>[0]", while Get does.

Here's a minimal reproducer:

package main

import (
	"fmt"

	"github.com/buger/jsonparser"
)

func main() {
	data := []byte(`{"request":{"headers":{"User-Agent":["abc"]}}}`)

	paths := [][]string{
		[]string{"request", "headers", "User-Agent", "[0]"},
	}

	jsonparser.EachKey(data, func(idx int, val []byte, typ jsonparser.ValueType, err error) {
		fmt.Printf("NO: val: %s / typ: %s / err: %s\n", val, typ, err)
	}, paths...)

	v, t, _, e := jsonparser.Get(data, paths[0]...)
	fmt.Printf("OK: val: %s / typ: %s / err: %s\n", v, t, e)
}

As you can see in the output EachKey fails while Get doesn't:

NO: val:  / typ: unknown / err: Unknown value type
OK: val: abc / typ: string / err: %!s(<nil>)
leodido added a commit to leodido/caddy-jsonselect-encoder that referenced this issue Jul 19, 2021
The bug is in the jsonparser: buger/jsonparser#232

Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
leodido added a commit to leodido/caddy-jsonselect-encoder that referenced this issue Jul 19, 2021
The bug is in the jsonparser: buger/jsonparser#232

Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
@cedecegreen
Copy link

this issue may be a duplicate of #180

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants