ohler55/ojg

Getting full path of a rule in the json

LironKS opened this issue · 3 comments

Hi,
Thank you for this contribution, it's very helpful :)

I'm looking for a method that for a given rule is returning a list of the full paths it appears in the json.
For example, my json is:
{ "name": "John Smith", "male": true, "age": 35, "address": "New York", "null": null, "metadata": { "date": "2022-04-01T00:00:00.000Z", "count": "5", "name": "John Smith" }, "test": { "name": "John Smith" } }

and the rule is: "$.*.name"

So I want to have something like this: ["$.metadata.name","$.test.name"]

Is there any method that does this?

Thanks.

There is not at this point in time.

This relates to #59

Does the jp Locate() function do what you were looking for?

It does.

package main

import (
	"fmt"
	"github.com/ohler55/ojg/jp"
	"github.com/ohler55/ojg/oj"
)

func main() {
	jsonDataString := `{
		"name": "John Smith",
		"male": true,
		"age": 35,
		"address": "New York",
		"null": null,
		"metadata": {
			"date": "2022-04-01T00:00:00.000Z",
			"count": "5",
			"name": "John Smith"
		},
		"test": {
			"name": "John Smith"
		}
	}`
	data, _ := oj.ParseString(jsonDataString)

	myJP := jp.MustParseString(`$.*.name`)
	myLoc := myJP.Locate(data, 0)
	fmt.Printf("%+v\n", myLoc)
}

Output

[$.metadata.name $.test.name]

Run on playgroud