rjson

package module
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: GPL-3.0 Imports: 9 Imported by: 3

README

rjson

a jq like library for golang that helps parse nested json

go get github.com/BatteredBunny/rjson

Parse nested json without any anonymous structs

type Out struct {
	One     string      `rjson:"uwu.nya"`           // "123"
	Two     int         `rjson:"one.two.three.num"` // 1
	Three   []string    `rjson:"one.arr"`           // ["a","b"]
    Four    string      `rjson:"one.arr[0]"`        // "a"
}
{
    "uwu": {
        "nya": "123"
    },
    "one": {
        "two": {
            "three": {
                "num": 1
            }
        },
        "arr": [
            "a",
            "b"
        ]
    }
}

For a more complete example have a look at tag_test.go

Helpful

Debugging

If the json isnt parsing as expected try enabling the rjson.Debug variable. rjson.Debug = true

Jetbrains

For quickly parsing json, in jetbrains IDE you can directly copy the json pointer and paste it into rjson field tag tip

copied value: onResponseReceivedActions[0].appendContinuationItemsAction.continuationItems[76].playlistVideoRenderer.thumbnail.thumbnails

Syntax explanation

Path seperator: "."

  • Dot symbol is used as path seperator, e.g one.two.three

Array index: [0]

  • You can index slices/array like you would normally, e.g arr[0], arr[1]

Last value: [-]

  • You can access the last value of an slices/array using this, e.g arr[-]

Value iterator: []

  • e.g arr[].text

    Input
    {
        "arr": [
            {
                "text": "1",
                "num": 1
            },
            {
                "text": "2",
                "num": 2
            },
            {
                "no_commons": "this object has no common fields, so it wont be included"
            }
        ]
    }
    
    Output
    [
        {
            "text": "1",
        },
        {
            "text": "2",
        }
    ]
    

Documentation

Index

Constants

View Source
const ArrayClose = "]"
View Source
const ArrayLast = "-"
View Source
const ArrayOpen = "["
View Source
const Divider = "."
View Source
const TagName = "rjson"

Variables

View Source
var Debug = false

Enables debug options

View Source
var ErrCantFindField = errors.New("cant find field")
View Source
var ErrFailedToParseTag = errors.New("failed to parse tag")
View Source
var ErrInvalidIndex = errors.New("invalid slice index")
View Source
var ErrMalformedSyntax = errors.New("malformed tag syntax")
View Source
var ErrNotAPointer = errors.New("please insert a pointer")
View Source
var ErrNotAnObject = errors.New("failed to parse as json object:")

Functions

func QueryJson

func QueryJson(data []byte, tag string) (object json.RawMessage, err error)

QueryJson is the underlying function powering the tag, accepts json as bytes

func Unmarshal

func Unmarshal(data []byte, v any) (err error)

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an ErrNotAPointer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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