github.com/theory/sqljson

PostgreSQL-compatible SQL-standard SQL/JSON in Go


Keywords
json, jsonpath, postgres, sql
License
PostgreSQL
Install
go get github.com/theory/sqljson

Documentation

Go SQL/JSON

License GoDoc Go Report Card Build Status Code Coverage

The SQL/JSON package provides PostgreSQL-compatible SQL-standard SQL/JSON functionality in Go. For now that means jsonpath. An example:

func main() {
    src := []byte(`{
      "track": {
        "segments": [
          {
            "location":   [ 47.763, 13.4034 ],
            "start time": "2018-10-14 10:05:14",
            "HR": 73
          },
          {
            "location":   [ 47.706, 13.2635 ],
            "start time": "2018-10-14 10:39:21",
            "HR": 135
          }
        ]
      }
    }`)

    // Parse the JSON.
    var value any
    if err := json.Unmarshal(src, &value); err != nil {
        log.Fatal(err)
    }

    // Parse the SQL-standard jsonpath query.
    p, err := path.Parse(`$.track.segments[*] ? (@.HR > 130)."start time"`)
    if err != nil {
        log.Fatal(err)
    }

    // Execute the query against the JSON.
    items, err := p.Query(context.Background(), value)
    if err != nil {
        log.Fatal(err)
    }

    // Print the results.
    fmt.Printf("%v\n", items)
    // Output: [2018-10-14 10:39:21]
}

See the path README for a complete description of the SQL/JSON path language, and the Go doc for usage and examples.

Or take the 🛝 Playground for a spin (direct link for above example). Implemented as a single-page stateless JavaScript and Go WebAssembly app.

Copyright

Copyright © 1996-2024 The PostgreSQL Global Development Group

Copyright © 2024 David E. Wheeler