jsonq

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 1 Imported by: 0

README

jsonq

jsonq

Go Report Card Release License GoDoc

Easily traverse through the JSON data with no fixed structure.

Installation

go get -u github.com/IamFaizanKhalid/jsonq

Usage Example

See reference docs for more details.

package main

import (
	"fmt"

	"github.com/IamFaizanKhalid/jsonq"
)

func main() {
	apiResponse := []byte(`{
		"code": 200,
		"status": "success",
		"data": {
			"user": {
				"id": 123,
				"name": "John Doe",
				"email": "john@example.com"
			},
			"posts": [
				{
					"id": 1,
					"title": "First Post"
				},
				{
					"id": 2,
					"title": "Second Post"
				}
			]
		}
	}`)

	// parsing the JSON object
	resp, err := jsonq.ParseObject(apiResponse)
	if err != nil {
		panic(err)
	}

	// getting values from the object
	code := resp.Val("code").Int()
	status := resp.Val("status").Str()

	fmt.Println(code, status)

	// checking if the object contains the sub-object
	if !resp.Has("data") {
		return
	}
	data := resp.Obj("data")

	// getting a sub-object which is optional
	userInfo, ok := data.OptObj("user")
	if ok {
		fmt.Println("-- User Info --")

		fmt.Println("ID:", userInfo.Val("id").Int())
		fmt.Println("Name:", userInfo.Val("name").Str())
		fmt.Println("Email:", userInfo.Val("email").Str())
	}

	fmt.Println("-- Posts --")

	// getting array of objects
	posts := data.Arr("posts").Obj()

	for _, post := range posts {
		id := post.Val("id").Int()
		title := post.Val("title").Str()

		fmt.Println(id, title)
	}
}

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array []any

func ParseArray

func ParseArray(b []byte) (Array, error)

func (Array) Arr

func (a Array) Arr() []Array

func (Array) Bool

func (a Array) Bool() []bool

func (Array) Float

func (a Array) Float() []float64

func (Array) Int

func (a Array) Int() []int

func (Array) Obj

func (a Array) Obj() []Object

func (Array) Str

func (a Array) Str() []string

type Object

type Object map[string]interface{}

func ParseObject

func ParseObject(b []byte) (Object, error)

func (Object) Arr

func (o Object) Arr(key string) Array

func (Object) Has

func (o Object) Has(key string) bool

func (Object) Obj

func (o Object) Obj(key string) Object

func (Object) OptArr

func (o Object) OptArr(key string) (Array, bool)

func (Object) OptObj

func (o Object) OptObj(key string) (Object, bool)

func (Object) OptVal

func (o Object) OptVal(key string) (Value, bool)

func (Object) Val

func (o Object) Val(key string) Value

type Value

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

func ParseValue

func ParseValue(b []byte) (Value, error)

func (Value) Bool

func (v Value) Bool() bool

func (Value) Float

func (v Value) Float() float64

func (Value) Int

func (v Value) Int() int

func (Value) OptBool

func (v Value) OptBool() (bool, bool)

func (Value) OptFloat

func (v Value) OptFloat() (float64, bool)

func (Value) OptInt

func (v Value) OptInt() (int, bool)

func (Value) OptStr

func (v Value) OptStr() (string, bool)

func (Value) Str

func (v Value) Str() string

Jump to

Keyboard shortcuts

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