Jeffail / gabs

For parsing, creating and editing unknown or dynamic JSON in Go
MIT License
3.45k stars 202 forks source link

Issue with finding values for key containing dot #85

Open Vaibzz opened 4 years ago

Vaibzz commented 4 years ago

I am facing issues while fetching keys having "." in them. Example code- https://play.golang.org/p/3vpVXQvA6fv

Please help.

Jeffail commented 4 years ago

Hey @Vaibzz, from the docs:

Because the characters '\~' (%x7E) and '.' (%x2E) have special meanings in gabs paths, '\~' needs to be encoded as '\~0' and '.' needs to be encoded as '\~1' when these characters appear in a reference key.

Your options are to encode dots as ~1 in your path arguments like this:

log.Println(jsonParsed.Path("prediction~1prediction_confidence").Data())

Or, alternatively, you can use the Search method which is an explicit slice of path segments:

log.Println(jsonParsed.Search("prediction.prediction_confidence").Data())

Vaibzz commented 4 years ago

Thanks @Jeffail . It works for me.

Vaibzz commented 4 years ago

@Jeffail Is there any performance degradation in Search method as compared to Path method?

Jeffail commented 4 years ago

No, in fact it should be faster if anything.