Categorygithub.com/qri-io/jsonpointer
modulepackage
0.1.1
Repository: https://github.com/qri-io/jsonpointer.git
Documentation: pkg.go.dev

# README

Qri GoDoc License Codecov CI Go Report Card

jsonpointer

golang implementation of IETF RFC6901: JSON Pointer defines a string syntax for identifying a specific value within a JavaScript Object Notation (JSON) document.

Installation

install with: go get -u github.com/qri-io/jsonpointer

Usage

Here's a quick example pulled from the godoc:

import (
  "encoding/json"
  "fmt"
  "github.com/qri-io/jsonpointer"
)

var document = []byte(`{ 
  "foo": {
    "bar": {
      "baz": [0,"hello!"]
    }
  }
}`)

func main() {
  parsed := map[string]interface{}{}
  // be sure to handle errors in real-world code!
  json.Unmarshal(document, &parsed)

  // parse a json pointer. Pointers can also be url fragments
  // the following are equivelent pointers:
  // "/foo/bar/baz/1"
  // "#/foo/bar/baz/1"
  // "http://example.com/document.json#/foo/bar/baz/1"
  ptr, _ := jsonpointer.Parse("/foo/bar/baz/1")

  // evaluate the pointer against the document
  // evaluation always starts at the root of the document
  got, _ := ptr.Eval(parsed)

  fmt.Println(got)
  // Output: hello!
}

License

MIT

Issues & Contributions

Contributions & Issues are more than welcome! Everything happens over on this repo's github page

# Functions

NewPointer creates a Pointer with a pre-allocated block of memory to avoid repeated slice expansions.
Parse parses str into a Pointer structure.
WalkJSON calls visit on all elements in a tree of decoded json.

# Interfaces

JSONContainer returns any existing child value for a given JSON property string.
JSONParent is an interface that enables tree traversal by listing all immediate children of an object.

# Type aliases

Pointer represents a parsed JSON pointer.