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

The fromstr modifier does not support special strings, such as ! #323

Closed
15083787153 opened this issue Apr 6, 2023 · 9 comments
Closed

Comments

@15083787153
Copy link

No description provided.

@15083787153
Copy link
Author

15083787153 commented Apr 6, 2023

this is my code:
`package main

import (
"fmt"

"github.com/tidwall/gjson"

)

func main() {
const jsonNew = {"!stringMember": "[{\"q!qq\": 123},{\"key2\": 2324}]"}
fmt.Println("result:", gjson.Get(jsonNew, !stringMember|@fromstr|#(q!qq).q!qq))
}
`

but the result is empty

@volans-
Copy link

volans- commented Apr 6, 2023

@15083787153 your message has some formatting problem, so I might have misunderstood, but it's probably just a problem of escaping the special character, see also the related doc.

With a JSON of:

{"!stringMember": "[{\"q!qq\": 123},{\"key2\": 2324}]"}

and a query of:

!stringMember|@fromstr|#(q\!qq).q!qq

you get: 123

Is that what yo were looking for?

@15083787153
Copy link
Author

15083787153 commented Apr 6, 2023

thank you so much! can you tell me all the special character?I need special handling for these special character,but I can't find it in the docs。

@volans-
Copy link

volans- commented Apr 6, 2023

For that you have to ask @tidwall, I don't think all of them are documented and they might depend on where you use them. For example ! is used for literals in the syntax but does't need to be escaped in the path !stringMember but it does in the query #(q\!qq).
That said, if you escape it in all places it works the same:

\!stringMember|@fromstr|#(q\!qq).q\!qq

@15083787153
Copy link
Author

@tidwall Do you know the range of special character? I need to do special processing in a specific area.

@15083787153
Copy link
Author

@volans- Or I want to get 123, is there any better way?

@15083787153
Copy link
Author

Through the documentation of gjson and my own practice, I found that these are special characters, but I don't know if there are more characters。
! ? * > < = \ % ( )

@volans-
Copy link

volans- commented Apr 7, 2023

@volans- Or I want to get 123, is there any better way?

It depends on your data and what you want, for example you can get all the values for the key q!qq with:

!stringMember|@fromstr|#.q!qq

that returns in this case:

[123]

If you know that you always want the first one then you can get it with:

!stringMember|@fromstr|#.q!qq|0

@15083787153
Copy link
Author

@volans- Or I want to get 123, is there any better way?

It depends on your data and what you want, for example you can get all the values for the key q!qq with:

!stringMember|@fromstr|#.q!qq

that returns in this case:

[123]

If you know that you always want the first one then you can get it with:

!stringMember|@fromstr|#.q!qq|0

Thank you very much! This is exactly what I need.

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

2 participants