Parse
Overview
Parse JSON into a typed value using generics — like JSON.parse but with compile-time type safety.
If you are coming from Node.js, the closest pattern is JSON.parse(str).
Signature
go
func Parse(data []byte, v any) errorCompare: Node.js · Standard Go · gox
js
const obj = JSON.parse(str);go
err := json.Unmarshal([]byte(str), &obj)go
import "github.com/sahilkhaire/gox/json"
obj, err := json.Parse[MyType](str)Example
go
import "github.com/sahilkhaire/gox/json"
obj, err := json.Parse[MyType](str)Tips
Use MustParse when invalid JSON should panic in init or tests.
Standard library alternative
Use the standard library directly:
go
err := json.Unmarshal([]byte(str), &obj)