Comments (3)
Thanks and I'm happy to hear that you find the library useful.
The default gjson.Result{}
value currently represents a non-existent json member. You can use the result.Exists()
function to see if the member exists, and if it does you can use if result.Type == gjson.Null
to check if the value is null
of something else.
Here's some examples:
result := gjson.Get(`{"first":"Jane","last":"Austen"}`, "last")
println(result.Exists()) // true
println(result.Type) // gjson.String
result := gjson.Get(`{"first":"Jane","last":null}`, "last")
println(result.Exists()) // true
println(result.Type) // gjson.Null
result := gjson.Get(`{"first":"Jane"}`, "last")
println(result.Exists()) // false
println(result.Type) // gjson.Null
from gjson.
Thanks @tidwall In hindsight, this was kind of a silly request. I'll just wrap this up in a function on our end. Ideally I'd like it to work like:
result, err := gjson.GetWithDefault(`{"first":"Jane","last":null}`, "last", "foo")
println(err) // nil
println(result.Exists()) // true
println(result.Type) // gjson.String
println(result) // "foo"
result, err := gjson.GetWithDefault(`{"first":"Jane"}`, "last", "foo")
println(err) // NoValueError
println(result.Exists()) // false
println(result.Type) // gjson.String
println(result) // "foo"
Note: Not sure exactly how we'd handle err. This is admittedly a little gross.
We use this to parse JSON, and we often find edge cases with missing data, or things we didn't understand. We'd like to be able to default basically everything to a known JSON string, and have it be parsed like it existed.
from gjson.
Not a silly request at all. I think your solution to wrap it in a function is a good one.
from gjson.
Related Issues (20)
- import "github.com/tidwall/gjson" is a program, not an importable package HOT 2
- Query for node that doesn't have specified field HOT 1
- Iterating only first N items in an array HOT 2
- Tilde operator treats values other than true and 1 as false HOT 3
- pretty modifiers are not working as expected HOT 2
- I need a set function to modify json string HOT 1
- What is the difference between raw and str HOT 1
- Querying nested optional array HOT 2
- Same problem as #259
- Accessing key with periods in it HOT 6
- How to use gjson's capabilities to convert json strings into objects HOT 1
- Flatten/Expand Array HOT 2
- join flat array to comma separated string HOT 2
- help with a query to get all paths in a nested json HOT 2
- forEach fo current json string HOT 4
- How to get next value under ForEach function HOT 2
- How to pick up specific values from array of objects into target array of objects? HOT 2
- How to apply multipaths to all elements of an array HOT 1
- Query based on content
- README.md references unknown anchor [GetMany](#get-multiple-values-at-once)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gjson.