-
Notifications
You must be signed in to change notification settings - Fork 857
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
How to query JSON including literal field/values in the output? #249
Comments
You can use the '!' character to define static json as a path component. For example, {name.last,"foo":!"bar"} => {name.last,"foo":"bar"} see #249
Right now everything is a path. There is no way to embed static json. That said, I think it would be pretty cool to have this functionality. I think it could be a nice extension to the library. Perhaps we can include a new character that indicates that the value is static json. In this case I'm using the '!' to tell gjson the value is static.
I pushed this feature for testing to the master branch and to the GJSON Playground. |
@tidwall Wow, that was fast! Fantastic! In the playground it has worked very well, I'll do a couple of tests in code but I think it surely will fit my use case. Thank you! |
@tidwall I wonder if it could be made without the declaration character "!" though...maybe using some sort of fallback: in case of inexistent source path and the token is a valid string/number, assume static content, the feature would be more intuitive for anyone using gjson. What do you think? Examples:
|
ok, on second thought it may not be possible due to the ambiguation over optional json fields and booleans :( |
I agree. I wish there was a way to avoid the declaration character. For strings, because they are wrapped in quotes, we may possibly get away with forgoing the I feel that to distinguish the literals from gjson paths, something like Perhaps there's a better way. I'm open to suggestions. |
@tidwall the feature worked really well for me. I'm closing this issue since it was shipped in the last minor release. Thank you again for the support! ;) |
I'm building a JSON transformation feature using GJSON and the use case is to parse an input and generate a transformed JSON, but also including extra fields (i.e. fields/values not present in the source JSON). Is that possible with GJSON?
Example:
Given the following input (from GJSON playground):
When the input is
{name.last,"foo":name.first}
, the result is{"last":"Anderson","foo":"Tom"}
But when the input is
{name.last,"foo":"bar"}
, the result is:{"last":"Anderson"}
I expected that the result would be
{"last":"Anderson","foo":"bar"}
Is there any way to achieve that?
The text was updated successfully, but these errors were encountered: