jsonpointergo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 3 Imported by: 0

README

jsonpointer-go

An implementation of RFC 6901 - JavaScript Object Notation (JSON) Pointer in Golang.

Table of Contents

Introduction

jsonpointer-go is a Go library for working with JSON Pointers as defined in RFC 6901. JSON Pointers provide a way to reference specific parts of a JSON document.

Installation

To install the library, simply run:

go get github.com/bragdond/jsonpointer-go

Usage

Here is a simple example of how to use jsonpointer-go:

package main

import (
    "fmt"
    "github.com/bragdond/jsonpointer-go"
)

func main() {   
	jsonStr := `{
        "foo": ["bar", "baz"],
        "qux": {"corge": "grault"}
    }`

	var jsonData map[string]any
	if err := json.Unmarshal([]byte(jsonStr), &jsonData); err != nil {
		panic(fmt.Sprintf("failed to unmarshal json string: %v", err))
	}

    pointer, err := jsonpointer.Parse("/foo/0")
    if err != nil {
        panic(err)
    }

    result, err := pointer.GetValue(jsonData)
    if err != nil {
        panic(err)
    }

    fmt.Println(result) // Output: bar
}

Contributing

We welcome contributions! Please see the CONTRIBUTING.md file for guidelines on how to contribute.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Happy coding!

Documentation

Overview

Package jsonpointer-go provides functions to use jsonpointer defined by the RFC 6902 in Golang.

Index

Constants

View Source
const (
	JSONPointerEmptyPointer   = ""
	JSONPointerSeparatorToken = "/"
	JSONPointerEscapeToken    = "~"
	JSONPointerSlashEncoded   = "~1"
	JSONPointerTildaEncoded   = "~0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONObject

type JSONObject = map[string]any

JSONObject is a type alias for a map with string keys and values of any type

type JSONPointer

type JSONPointer struct {
	// contains filtered or unexported fields
}

JSONPointer struct holds the parsed reference tokens of a JSON Pointer

func NewJSONPointer

func NewJSONPointer(jsonPointer string) (*JSONPointer, error)

NewJSONPointer creates a new JSONPointer instance from a JSON Pointer string

func (*JSONPointer) GetValue

func (jp *JSONPointer) GetValue(document JSONObject) (any, error)

GetValue retrieves the value from the JSON document based on the JSON Pointer

Jump to

Keyboard shortcuts

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