tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.39k stars 857 forks source link

Accessing key with periods in it #333

Open rowsimms42 opened 1 year ago

rowsimms42 commented 1 year ago

Having keys in the JSON that contain periods will cause incorrect results when using the "Get" function. It would be nice if you could provide the path as Get(key1, key2) instead of as a combined string, or if there were simply a "GetKey" function that considered the passed in string to be the key instead of potentially a path.

volans- commented 1 year ago

@rowsimms42 did you escape the periods? See https://github.com/tidwall/gjson/blob/master/SYNTAX.md#escape-character

rowsimms42 commented 1 year ago

@rowsimms42 did you escape the periods? See https://github.com/tidwall/gjson/blob/master/SYNTAX.md#escape-character

that would only work if I rebuilt the key. I am matching keys in two objects, and am looping through one object and calling "otherObj.Get(key)"

volans- commented 1 year ago

@rowsimms42 got it. I've never used it and I'm not sure if it's meant to be part of the public API, but there is an escapeComp function that seems to just do what you need, so basically calling it on the key you are passing o otherObj to get it properly escaped. Again, it might be meant to be internal use only and I can't guarantee it will not be renamed/changed in later releases. You'll have to check with @tidwall.

tidwall commented 1 year ago

I just exposed the escapeComp as Escape.

otherObj.Get(gjson.Escape(key))
rowsimms42 commented 1 year ago

I just exposed the escapeComp as Escape.

otherObj.Get(gjson.Escape(key))

thank you!

tidwall commented 1 year ago

No problem and thanks @volans- for finding that function. I forgot it existed.