jsonpath

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 4 Imported by: 0

README

go-jsonpath

CI Go Reference License

JSONPath query and extraction for Go. Type-safe with generics, zero dependencies

Installation

go get github.com/philiprehberger/go-jsonpath

Usage

Extract Values
import "github.com/philiprehberger/go-jsonpath"

data := []byte(`{"user": {"name": "Alice", "age": 30}}`)

name, err := jsonpath.Get[string](data, "$.user.name")
// name = "Alice"

age, err := jsonpath.Get[float64](data, "$.user.age")
// age = 30
Wildcards
data := []byte(`{"users": [{"name": "Alice"}, {"name": "Bob"}]}`)

names, err := jsonpath.GetAll[string](data, "$.users[*].name")
// names = ["Alice", "Bob"]
Set Values
data := []byte(`{"name": "Alice"}`)

updated, err := jsonpath.Set(data, "$.name", "Bob")
// updated = {"name": "Bob"}
Exists
import jsonpath "github.com/philiprehberger/go-jsonpath"

exists, _ := jsonpath.Exists(data, "$.address.city")
// true
Delete
import jsonpath "github.com/philiprehberger/go-jsonpath"

result, _ := jsonpath.Delete(data, "$.temporary")
Supported Syntax
Syntax Description
$ Root object
$.field Object field
$.a.b.c Nested fields
$[0] Array index
$.items[*] Wildcard (all elements)

API

Function Description
Get[T](data, path) Extract single value as type T
GetRaw(data, path) Extract value without type conversion
GetAll[T](data, path) Extract all wildcard matches as type T
Set(data, path, value) Set value at path, return modified JSON
Exists(data, path) Check whether a path exists
Delete(data, path) Remove value at path, return modified JSON

Development

go test ./...
go vet ./...

License

MIT

Documentation

Overview

Package jsonpath provides JSONPath query and extraction for Go. It supports type-safe extraction with generics, array indexing, wildcards, and setting values at paths. Zero external dependencies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete added in v0.2.0

func Delete(data []byte, path string) ([]byte, error)

Delete removes the value at the given path from the JSON data.

func Exists added in v0.2.0

func Exists(data []byte, path string) (bool, error)

Exists checks whether a path exists in the JSON data.

func Get

func Get[T any](data []byte, path string) (T, error)

Get extracts a single value at the given JSONPath and unmarshals it to type T. Returns an error if the path is invalid, not found, or the value cannot be converted to T.

func GetAll

func GetAll[T any](data []byte, path string) ([]T, error)

GetAll extracts all matching values for a wildcard JSONPath and unmarshals each to type T. Use this with paths containing [*].

func GetRaw

func GetRaw(data []byte, path string) (any, error)

GetRaw extracts a value at the given JSONPath without type conversion. The returned value is one of: nil, bool, float64, string, []any, or map[string]any.

func Set

func Set(data []byte, path string, value any) ([]byte, error)

Set sets a value at the given JSONPath and returns the modified JSON. The path must point to an existing location or a direct child of an existing object/array.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL