Skip to content

How to get all matches with Paths #382

@keminar

Description

@keminar

code

package main

import (
	"fmt"

	"github.com/tidwall/gjson"
)

func main() {
	ret := []byte(`{
		"result": {
			"data": [
				{"data": [{"type": "quan", "value": 1}, {"type": "quan", "value": 2}, {"type": "quan", "value": 3},  {"type": "other", "value": 4}]},
				{"data": [{"type": "quan", "value": 3}]}
			]
		}
	}`)

	path := "result.data.#.data.#(type==\"quan\")"
	result := gjson.GetBytes(ret, path)
	fmt.Println(result, result.Paths(string(ret)))

	path = "result.data.#.data.#(type==\"quan\")#"
	result = gjson.GetBytes(ret, path)
	fmt.Println(result, result.Paths(string(ret)))

}

output

[{"type": "quan", "value": 1},{"type": "quan", "value": 3}] [result.data.0.data.0 result.data.1.data.0]
[[{"type": "quan", "value": 1},{"type": "quan", "value": 2},{"type": "quan", "value": 3}],[{"type": "quan", "value": 3}]] [ ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions