gjson icon indicating copy to clipboard operation
gjson copied to clipboard

Accessing key with periods in it

Open rowsimms42 opened this issue 2 years ago • 6 comments

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.

rowsimms42 avatar Sep 22 '23 14:09 rowsimms42

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

volans- avatar Sep 22 '23 15:09 volans-

@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)"

rowsimms42 avatar Sep 22 '23 15:09 rowsimms42

@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.

volans- avatar Sep 22 '23 15:09 volans-

I just exposed the escapeComp as Escape.

otherObj.Get(gjson.Escape(key))

tidwall avatar Sep 22 '23 17:09 tidwall

I just exposed the escapeComp as Escape.

otherObj.Get(gjson.Escape(key))

thank you!

rowsimms42 avatar Sep 22 '23 17:09 rowsimms42

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

tidwall avatar Sep 22 '23 17:09 tidwall