Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing key with periods in it #333

Open
rowsimms42 opened this issue Sep 22, 2023 · 6 comments
Open

Accessing key with periods in it #333

rowsimms42 opened this issue Sep 22, 2023 · 6 comments

Comments

@rowsimms42
Copy link

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-
Copy link

volans- commented Sep 22, 2023

@rowsimms42
Copy link
Author

@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-
Copy link

volans- commented Sep 22, 2023

@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 added a commit that referenced this issue Sep 22, 2023
This commit adds the Escape function for escaping a path
component, making it possible to directly querying keys that have
special characters like dots.

```
json := `{
  "user":{
      "first.name": "Janet",
      "last.name": "Prichard"
    }
}`
user := gjson.Get(json, "user")
println(user.Get(gjson.Escape("first.name")).String())
println(user.Get(gjson.Escape("last.name")).String())
// Output:
// Janet
// Prichard
```

See #333
@tidwall
Copy link
Owner

tidwall commented Sep 22, 2023

I just exposed the escapeComp as Escape.

otherObj.Get(gjson.Escape(key))

@rowsimms42
Copy link
Author

I just exposed the escapeComp as Escape.

otherObj.Get(gjson.Escape(key))

thank you!

@tidwall
Copy link
Owner

tidwall commented Sep 22, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants