jsontree

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsontree provides types and utilities for working with parsed JSON value trees — the Go representation produced by json.Unmarshal into any.

A Tree is a type alias for any, constrained by convention to the shapes that encoding/json produces:

  • map[string]any (JSON object)
  • []any (JSON array)
  • string (JSON string)
  • float64 (JSON number)
  • bool (JSON boolean)
  • nil (JSON null)

The tree shape is not JSON-specific — any format that can be parsed into map[string]any / []any (YAML, MessagePack, etc.) is compatible with the utilities in this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeletePaths

func DeletePaths(tree Tree, paths []string)

DeletePaths deletes the specified dot-separated paths from the tree in-place. Supports "#" as a wildcard segment for iterating array elements. The caller is responsible for copying the tree first if mutation is not desired.

func WalkPath

func WalkPath(tree Tree, path string, visitor func(parent map[string]any, key string))

WalkPath navigates a map[string]any / []any tree along a dot-separated path and calls visitor(parent, key) at each matching leaf. The visitor receives the parent map and the final key so it can read, write, or delete the entry.

The "#" segment is a wildcard that iterates over []any slices, applying the remaining path to each element.

Missing intermediate keys are silently skipped. If the path leads to a non-existent key, the visitor is not called.

Types

type Tree

type Tree = any

Tree is a Go value tree produced by json.Unmarshal into any. Valid shapes: map[string]any (object), []any (array), string, float64, bool, nil.

This is a type alias (not a defined type), so it is fully interchangeable with any at call sites — no casting or wrapping required.

func DeepCopy

func DeepCopy(v Tree) Tree

DeepCopy creates a deep copy of a JSON value tree to avoid aliasing. Maps and slices are recursively copied; scalars (string, float64, bool, nil) are returned as-is since they are immutable.

func PickPaths

func PickPaths(tree Tree, paths []string) Tree

PickPaths builds a new tree containing only the values at the specified dot-separated paths. The source tree is never mutated. Supports "#" as a wildcard segment for iterating array elements. Returns an empty map (not nil) when no paths match — this is intentional so the result is always a valid tree for downstream operations.

Jump to

Keyboard shortcuts

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