jsonedit

package
v0.0.0-...-2d09b37 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 5 Imported by: 1 module, 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 jsonedit makes minimal text edits to a JSON document: it finds the value at a path and replaces, inserts or deletes it while every other byte of the document stays as it was, including indentation, key order and line ends. It is the Go counterpart of the VSCode extension's jsonc-parser "modify" (src/docEdits.ts), so a file edited by a tool and by the editor shows the same small diff.

A path is a list of steps: a string selects the member of an object, an int the element of an array. The document must be valid JSON without comments. Values passed in are JSON text; they are laid out in the document's style: in one line (`{"a": 1, "b": 2}`) where the value they replace or the container they enter is on one line, else spread over lines with the document's own indentation. The package imports the standard library only.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("jsonedit: no value at path")

ErrNotFound is returned (wrapped) when a path leads to nothing.

Functions

func Delete

func Delete(doc []byte, path ...any) ([]byte, error)

Delete removes the object member or array element at path, with its comma, and returns doc. The path must exist and not be empty.

func Insert

func Insert(doc, value []byte, index int, path ...any) ([]byte, error)

Insert adds value as element index of the array at path; index -1 or the array's length appends.

func Replace

func Replace(doc, value []byte, path ...any) ([]byte, error)

Replace returns doc with the value at path replaced by value. The path must exist.

func Set

func Set(doc, value []byte, path ...any) ([]byte, error)

Set puts value at path: it replaces the value when the path exists, else it adds the missing object member (and the missing objects above it, when the path is deeper) at the end of the nearest existing object, or appends to an array when the last step is an index equal to the array's length.

Types

type Span

type Span struct{ Start, End int }

Span is the byte range [Start, End) of a value in a document.

func Find

func Find(doc []byte, path ...any) (Span, error)

Find returns the span of the value at path. The empty path is the whole document value.

Jump to

Keyboard shortcuts

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