Giter Site home page Giter Site logo

Defaults for Nil Case about gjson HOT 3 CLOSED

tidwall avatar tidwall commented on May 12, 2024
Defaults for Nil Case

from gjson.

Comments (3)

tidwall avatar tidwall commented on May 12, 2024

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.

svperfecta avatar svperfecta commented on May 12, 2024

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.

tidwall avatar tidwall commented on May 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.