jq

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2022 License: GPL-3.0 Imports: 5 Imported by: 2

README

jq

🍪 Buy me a cookie

Go Report Card

What is this?

This Go module allows you to parse JSON blobs in a "sane" manner.

How to install

Open a terminal and run the following:

$ go get --ldflags="-s -w" --trimpath -u gitlab.com/mjwhitta/jq

Usage

package main

import (
    "fmt"
    "os"
    "strings"

    "gitlab.com/mjwhitta/jq"
)

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println(r.(error).Error())
        }
    }()

    var e error
    var j *jq.JSON
    var json string
    var prettyJSON string
    var keys []string

    json = strings.Join(
        []string{
            "{",
            "  \"a\": true,",
            "  \"b\": \"asdf\",",
            "  \"c\": 1234,",
            "  \"d\": [",
            "    \"blah\",",
            "    \"test\"",
            "  ],",
            "  \"e\": {",
            "    \"aFloat\": 1.2,",
            "    \"anInt\": 0,",
            "    \"more\": {",
            "      \"aFloat32\": 1.2,",
            "      \"anInt64\": 0",
            "    }",
            "  }",
            "}",
        },
        "\n",
    )

    // Initialize JSON object
    if j, e = jq.New(json); e != nil {
        fmt.Println(e.Error())
        os.Exit(0)
    }

    // JSON blob
    if json, e = j.GetBlob(); e != nil {
        fmt.Println(e.Error())
    }
    prettyJSON = j.String()

    fmt.Println(json)
    fmt.Println(prettyJSON)

    // Error checking
    if j.HasKey("a") {
        if _, e = j.MustGetBool("b"); e != nil {
            fmt.Println(e.Error())
        }
        if _, e = j.MustGetBool("a"); e != nil {
            fmt.Println(e.Error())
        } else {
            fmt.Println("a is a bool")
        }
    }

    // Top-level keys
    fmt.Printf("a = %v\n", j.GetBool("a"))
    fmt.Printf("b = %v\n", j.GetString("b"))
    fmt.Printf("c = %v\n", j.GetInt("c"))
    fmt.Printf("d = %v\n", j.GetStringArray("d"))
    fmt.Printf("e = %v\n", j.GetMap("e"))

    // Nested keys
    fmt.Printf("f = %v\n", j.GetString("d", 0))
    fmt.Printf("g = %v\n", j.GetString("d", 1))
    fmt.Printf("h = %v\n", j.GetInt64("e", "anInt"))
    fmt.Printf("i = %v\n", j.GetFloat64("e", "aFloat"))

    // Get sub-keys
    if keys, e = j.MustGetKeys("a"); e != nil {
        fmt.Println(e.Error())
    } else {
        fmt.Println(keys)
    }

    keys = j.GetKeys("d")
    fmt.Println(keys)

    keys = j.GetKeys("e")
    fmt.Println(keys)

    keys = j.GetKeys("e", "more")
    fmt.Println(keys)

    // Set keys
    if e = j.Set(false, "asdf", "test"); e != nil {
        fmt.Println(e.Error())
    }
    j.Set(false, "asdf")

    j.Set(false, "a")

    if e = j.Set("asdf", "d", "asdf"); e != nil {
        fmt.Println(e.Error())
    }
    j.Set("asdf", "d", 1)

    if e = j.Set(17, "e", 0); e != nil {
        fmt.Println(e.Error())
    }
    j.Set(17, "e", "anInt")
    j.Set(19, "e", "more", "anInt64")

    fmt.Println(j.String())
}

Documentation

Overview

Code generated by scripts/generate_go_funcs; DO NOT EDIT.

Index

Constants

View Source
const Version = "1.5.2"

Version is the package version.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

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

JSON is a struct that holds a JSON blob.

func New

func New(blob ...string) (j *JSON, e error)

New is a JSON constructor.

func (*JSON) Append added in v1.2.3

func (j *JSON) Append(value interface{}, keys ...interface{}) error

Append will append the specified value to the specified key in the JSON blob, if it is an array.

func (*JSON) Clear added in v1.2.6

func (j *JSON) Clear()

Clear will reset the JSON blob to {}.

func (*JSON) Get

func (j *JSON) Get(keys ...interface{}) (ret interface{})

Get will return the value for the specified key(s) as a interface{}.

func (*JSON) GetArray

func (j *JSON) GetArray(keys ...interface{}) (ret []interface{})

GetArray will return an array for the specified key(s) as an []interface{}.

func (*JSON) GetBlob

func (j *JSON) GetBlob(params ...string) (ret string, e error)

GetBlob will return the JSON blob as a string. An indentation string and a prefix string are accepted as optionally parameters.

func (*JSON) GetBool

func (j *JSON) GetBool(keys ...interface{}) (ret bool)

GetBool will return the value for the specified key(s) as a bool.

func (*JSON) GetBoolArray

func (j *JSON) GetBoolArray(keys ...interface{}) (ret []bool)

GetBoolArray will return an array for the specified key(s) as a []bool.

func (*JSON) GetBoolMap

func (j *JSON) GetBoolMap(keys ...interface{}) (ret map[string]bool)

GetBoolMap will return a map for the specified key(s) as a map[string]bool.

func (*JSON) GetFloat32

func (j *JSON) GetFloat32(keys ...interface{}) (ret float32)

GetFloat32 will return the value for the specified key(s) as a float32.

func (*JSON) GetFloat32Array

func (j *JSON) GetFloat32Array(keys ...interface{}) (ret []float32)

GetFloat32Array will return an array for the specified key(s) as a []float32.

func (*JSON) GetFloat32Map

func (j *JSON) GetFloat32Map(keys ...interface{}) (ret map[string]float32)

GetFloat32Map will return a map for the specified key(s) as a map[string]float32.

func (*JSON) GetFloat64

func (j *JSON) GetFloat64(keys ...interface{}) (ret float64)

GetFloat64 will return the value for the specified key(s) as a float64.

func (*JSON) GetFloat64Array

func (j *JSON) GetFloat64Array(keys ...interface{}) (ret []float64)

GetFloat64Array will return an array for the specified key(s) as a []float64.

func (*JSON) GetFloat64Map

func (j *JSON) GetFloat64Map(keys ...interface{}) (ret map[string]float64)

GetFloat64Map will return a map for the specified key(s) as a map[string]float64.

func (*JSON) GetInt

func (j *JSON) GetInt(keys ...interface{}) (ret int)

GetInt will return the value for the specified key(s) as a int.

func (*JSON) GetInt16

func (j *JSON) GetInt16(keys ...interface{}) (ret int16)

GetInt16 will return the value for the specified key(s) as a int16.

func (*JSON) GetInt16Array

func (j *JSON) GetInt16Array(keys ...interface{}) (ret []int16)

GetInt16Array will return an array for the specified key(s) as a []int16.

func (*JSON) GetInt16Map

func (j *JSON) GetInt16Map(keys ...interface{}) (ret map[string]int16)

GetInt16Map will return a map for the specified key(s) as a map[string]int16.

func (*JSON) GetInt32

func (j *JSON) GetInt32(keys ...interface{}) (ret int32)

GetInt32 will return the value for the specified key(s) as a int32.

func (*JSON) GetInt32Array

func (j *JSON) GetInt32Array(keys ...interface{}) (ret []int32)

GetInt32Array will return an array for the specified key(s) as a []int32.

func (*JSON) GetInt32Map

func (j *JSON) GetInt32Map(keys ...interface{}) (ret map[string]int32)

GetInt32Map will return a map for the specified key(s) as a map[string]int32.

func (*JSON) GetInt64

func (j *JSON) GetInt64(keys ...interface{}) (ret int64)

GetInt64 will return the value for the specified key(s) as a int64.

func (*JSON) GetInt64Array

func (j *JSON) GetInt64Array(keys ...interface{}) (ret []int64)

GetInt64Array will return an array for the specified key(s) as a []int64.

func (*JSON) GetInt64Map

func (j *JSON) GetInt64Map(keys ...interface{}) (ret map[string]int64)

GetInt64Map will return a map for the specified key(s) as a map[string]int64.

func (*JSON) GetInt8 added in v1.2.9

func (j *JSON) GetInt8(keys ...interface{}) (ret int8)

GetInt8 will return the value for the specified key(s) as a int8.

func (*JSON) GetInt8Array added in v1.2.9

func (j *JSON) GetInt8Array(keys ...interface{}) (ret []int8)

GetInt8Array will return an array for the specified key(s) as a []int8.

func (*JSON) GetInt8Map added in v1.2.9

func (j *JSON) GetInt8Map(keys ...interface{}) (ret map[string]int8)

GetInt8Map will return a map for the specified key(s) as a map[string]int8.

func (*JSON) GetIntArray

func (j *JSON) GetIntArray(keys ...interface{}) (ret []int)

GetIntArray will return an array for the specified key(s) as a []int.

func (*JSON) GetIntMap

func (j *JSON) GetIntMap(keys ...interface{}) (ret map[string]int)

GetIntMap will return a map for the specified key(s) as a map[string]int.

func (*JSON) GetKeys added in v1.1.1

func (j *JSON) GetKeys(keys ...interface{}) (ret []string)

GetKeys will return a list of valid keys if the specified key returns an array or map.

func (*JSON) GetMap

func (j *JSON) GetMap(keys ...interface{}) (ret map[string]interface{})

GetMap will return a map for the specified key(s) as a map[string]interface{}.

func (*JSON) GetString

func (j *JSON) GetString(keys ...interface{}) (ret string)

GetString will return the value for the specified key(s) as a string.

func (*JSON) GetStringArray

func (j *JSON) GetStringArray(keys ...interface{}) (ret []string)

GetStringArray will return an array for the specified key(s) as a []string.

func (*JSON) GetStringMap

func (j *JSON) GetStringMap(keys ...interface{}) (ret map[string]string)

GetStringMap will return a map for the specified key(s) as a map[string]string.

func (*JSON) GetUint

func (j *JSON) GetUint(keys ...interface{}) (ret uint)

GetUint will return the value for the specified key(s) as a uint.

func (*JSON) GetUint16

func (j *JSON) GetUint16(keys ...interface{}) (ret uint16)

GetUint16 will return the value for the specified key(s) as a uint16.

func (*JSON) GetUint16Array

func (j *JSON) GetUint16Array(keys ...interface{}) (ret []uint16)

GetUint16Array will return an array for the specified key(s) as a []uint16.

func (*JSON) GetUint16Map

func (j *JSON) GetUint16Map(keys ...interface{}) (ret map[string]uint16)

GetUint16Map will return a map for the specified key(s) as a map[string]uint16.

func (*JSON) GetUint32

func (j *JSON) GetUint32(keys ...interface{}) (ret uint32)

GetUint32 will return the value for the specified key(s) as a uint32.

func (*JSON) GetUint32Array

func (j *JSON) GetUint32Array(keys ...interface{}) (ret []uint32)

GetUint32Array will return an array for the specified key(s) as a []uint32.

func (*JSON) GetUint32Map

func (j *JSON) GetUint32Map(keys ...interface{}) (ret map[string]uint32)

GetUint32Map will return a map for the specified key(s) as a map[string]uint32.

func (*JSON) GetUint64

func (j *JSON) GetUint64(keys ...interface{}) (ret uint64)

GetUint64 will return the value for the specified key(s) as a uint64.

func (*JSON) GetUint64Array

func (j *JSON) GetUint64Array(keys ...interface{}) (ret []uint64)

GetUint64Array will return an array for the specified key(s) as a []uint64.

func (*JSON) GetUint64Map

func (j *JSON) GetUint64Map(keys ...interface{}) (ret map[string]uint64)

GetUint64Map will return a map for the specified key(s) as a map[string]uint64.

func (*JSON) GetUint8 added in v1.2.9

func (j *JSON) GetUint8(keys ...interface{}) (ret uint8)

GetUint8 will return the value for the specified key(s) as a uint8.

func (*JSON) GetUint8Array added in v1.2.9

func (j *JSON) GetUint8Array(keys ...interface{}) (ret []uint8)

GetUint8Array will return an array for the specified key(s) as a []uint8.

func (*JSON) GetUint8Map added in v1.2.9

func (j *JSON) GetUint8Map(keys ...interface{}) (ret map[string]uint8)

GetUint8Map will return a map for the specified key(s) as a map[string]uint8.

func (*JSON) GetUintArray

func (j *JSON) GetUintArray(keys ...interface{}) (ret []uint)

GetUintArray will return an array for the specified key(s) as a []uint.

func (*JSON) GetUintMap

func (j *JSON) GetUintMap(keys ...interface{}) (ret map[string]uint)

GetUintMap will return a map for the specified key(s) as a map[string]uint.

func (*JSON) HasKey added in v1.1.1

func (j *JSON) HasKey(keys ...interface{}) bool

HasKey will return true if the JSON blob has the specified key, false otherwise.

func (*JSON) MustGet added in v1.2.0

func (j *JSON) MustGet(keys ...interface{}) (interface{}, error)

MustGet will return the value for the specified key(s) as a interface{}.

func (*JSON) MustGetArray added in v1.2.0

func (j *JSON) MustGetArray(
	keys ...interface{},
) (ret []interface{}, e error)

MustGetArray will return an array for the specified key(s) as an []interface{}.

func (*JSON) MustGetBool added in v1.2.0

func (j *JSON) MustGetBool(keys ...interface{}) (ret bool, e error)

MustGetBool will return the value for the specified key(s) as a bool.

func (*JSON) MustGetBoolArray added in v1.2.0

func (j *JSON) MustGetBoolArray(keys ...interface{}) (ret []bool, e error)

MustGetBoolArray will return an array for the specified key(s) as a []bool.

func (*JSON) MustGetBoolMap added in v1.2.0

func (j *JSON) MustGetBoolMap(keys ...interface{}) (ret map[string]bool, e error)

MustGetBoolMap will return a map for the specified key(s) as a map[string]bool.

func (*JSON) MustGetFloat32 added in v1.2.0

func (j *JSON) MustGetFloat32(keys ...interface{}) (ret float32, e error)

MustGetFloat32 will return the value for the specified key(s) as a float32.

func (*JSON) MustGetFloat32Array added in v1.2.0

func (j *JSON) MustGetFloat32Array(keys ...interface{}) (ret []float32, e error)

MustGetFloat32Array will return an array for the specified key(s) as a []float32.

func (*JSON) MustGetFloat32Map added in v1.2.0

func (j *JSON) MustGetFloat32Map(keys ...interface{}) (ret map[string]float32, e error)

MustGetFloat32Map will return a map for the specified key(s) as a map[string]float32.

func (*JSON) MustGetFloat64 added in v1.2.0

func (j *JSON) MustGetFloat64(keys ...interface{}) (ret float64, e error)

MustGetFloat64 will return the value for the specified key(s) as a float64.

func (*JSON) MustGetFloat64Array added in v1.2.0

func (j *JSON) MustGetFloat64Array(keys ...interface{}) (ret []float64, e error)

MustGetFloat64Array will return an array for the specified key(s) as a []float64.

func (*JSON) MustGetFloat64Map added in v1.2.0

func (j *JSON) MustGetFloat64Map(keys ...interface{}) (ret map[string]float64, e error)

MustGetFloat64Map will return a map for the specified key(s) as a map[string]float64.

func (*JSON) MustGetInt added in v1.2.0

func (j *JSON) MustGetInt(keys ...interface{}) (ret int, e error)

MustGetInt will return the value for the specified key(s) as a int.

func (*JSON) MustGetInt16 added in v1.2.0

func (j *JSON) MustGetInt16(keys ...interface{}) (ret int16, e error)

MustGetInt16 will return the value for the specified key(s) as a int16.

func (*JSON) MustGetInt16Array added in v1.2.0

func (j *JSON) MustGetInt16Array(keys ...interface{}) (ret []int16, e error)

MustGetInt16Array will return an array for the specified key(s) as a []int16.

func (*JSON) MustGetInt16Map added in v1.2.0

func (j *JSON) MustGetInt16Map(keys ...interface{}) (ret map[string]int16, e error)

MustGetInt16Map will return a map for the specified key(s) as a map[string]int16.

func (*JSON) MustGetInt32 added in v1.2.0

func (j *JSON) MustGetInt32(keys ...interface{}) (ret int32, e error)

MustGetInt32 will return the value for the specified key(s) as a int32.

func (*JSON) MustGetInt32Array added in v1.2.0

func (j *JSON) MustGetInt32Array(keys ...interface{}) (ret []int32, e error)

MustGetInt32Array will return an array for the specified key(s) as a []int32.

func (*JSON) MustGetInt32Map added in v1.2.0

func (j *JSON) MustGetInt32Map(keys ...interface{}) (ret map[string]int32, e error)

MustGetInt32Map will return a map for the specified key(s) as a map[string]int32.

func (*JSON) MustGetInt64 added in v1.2.0

func (j *JSON) MustGetInt64(keys ...interface{}) (ret int64, e error)

MustGetInt64 will return the value for the specified key(s) as a int64.

func (*JSON) MustGetInt64Array added in v1.2.0

func (j *JSON) MustGetInt64Array(keys ...interface{}) (ret []int64, e error)

MustGetInt64Array will return an array for the specified key(s) as a []int64.

func (*JSON) MustGetInt64Map added in v1.2.0

func (j *JSON) MustGetInt64Map(keys ...interface{}) (ret map[string]int64, e error)

MustGetInt64Map will return a map for the specified key(s) as a map[string]int64.

func (*JSON) MustGetInt8 added in v1.2.9

func (j *JSON) MustGetInt8(keys ...interface{}) (ret int8, e error)

MustGetInt8 will return the value for the specified key(s) as a int8.

func (*JSON) MustGetInt8Array added in v1.2.9

func (j *JSON) MustGetInt8Array(keys ...interface{}) (ret []int8, e error)

MustGetInt8Array will return an array for the specified key(s) as a []int8.

func (*JSON) MustGetInt8Map added in v1.2.9

func (j *JSON) MustGetInt8Map(keys ...interface{}) (ret map[string]int8, e error)

MustGetInt8Map will return a map for the specified key(s) as a map[string]int8.

func (*JSON) MustGetIntArray added in v1.2.0

func (j *JSON) MustGetIntArray(keys ...interface{}) (ret []int, e error)

MustGetIntArray will return an array for the specified key(s) as a []int.

func (*JSON) MustGetIntMap added in v1.2.0

func (j *JSON) MustGetIntMap(keys ...interface{}) (ret map[string]int, e error)

MustGetIntMap will return a map for the specified key(s) as a map[string]int.

func (*JSON) MustGetKeys added in v1.2.0

func (j *JSON) MustGetKeys(
	keys ...interface{},
) (ret []string, e error)

MustGetKeys will return a list of valid keys if the specified key returns an array or map.

func (*JSON) MustGetMap added in v1.2.0

func (j *JSON) MustGetMap(
	keys ...interface{},
) (ret map[string]interface{}, e error)

MustGetMap will return a map for the specified key(s) as a map[string]interface{}.

func (*JSON) MustGetString added in v1.2.0

func (j *JSON) MustGetString(keys ...interface{}) (ret string, e error)

MustGetString will return the value for the specified key(s) as a string.

func (*JSON) MustGetStringArray added in v1.2.0

func (j *JSON) MustGetStringArray(keys ...interface{}) (ret []string, e error)

MustGetStringArray will return an array for the specified key(s) as a []string.

func (*JSON) MustGetStringMap added in v1.2.0

func (j *JSON) MustGetStringMap(keys ...interface{}) (ret map[string]string, e error)

MustGetStringMap will return a map for the specified key(s) as a map[string]string.

func (*JSON) MustGetUint added in v1.2.0

func (j *JSON) MustGetUint(keys ...interface{}) (ret uint, e error)

MustGetUint will return the value for the specified key(s) as a uint.

func (*JSON) MustGetUint16 added in v1.2.0

func (j *JSON) MustGetUint16(keys ...interface{}) (ret uint16, e error)

MustGetUint16 will return the value for the specified key(s) as a uint16.

func (*JSON) MustGetUint16Array added in v1.2.0

func (j *JSON) MustGetUint16Array(keys ...interface{}) (ret []uint16, e error)

MustGetUint16Array will return an array for the specified key(s) as a []uint16.

func (*JSON) MustGetUint16Map added in v1.2.0

func (j *JSON) MustGetUint16Map(keys ...interface{}) (ret map[string]uint16, e error)

MustGetUint16Map will return a map for the specified key(s) as a map[string]uint16.

func (*JSON) MustGetUint32 added in v1.2.0

func (j *JSON) MustGetUint32(keys ...interface{}) (ret uint32, e error)

MustGetUint32 will return the value for the specified key(s) as a uint32.

func (*JSON) MustGetUint32Array added in v1.2.0

func (j *JSON) MustGetUint32Array(keys ...interface{}) (ret []uint32, e error)

MustGetUint32Array will return an array for the specified key(s) as a []uint32.

func (*JSON) MustGetUint32Map added in v1.2.0

func (j *JSON) MustGetUint32Map(keys ...interface{}) (ret map[string]uint32, e error)

MustGetUint32Map will return a map for the specified key(s) as a map[string]uint32.

func (*JSON) MustGetUint64 added in v1.2.0

func (j *JSON) MustGetUint64(keys ...interface{}) (ret uint64, e error)

MustGetUint64 will return the value for the specified key(s) as a uint64.

func (*JSON) MustGetUint64Array added in v1.2.0

func (j *JSON) MustGetUint64Array(keys ...interface{}) (ret []uint64, e error)

MustGetUint64Array will return an array for the specified key(s) as a []uint64.

func (*JSON) MustGetUint64Map added in v1.2.0

func (j *JSON) MustGetUint64Map(keys ...interface{}) (ret map[string]uint64, e error)

MustGetUint64Map will return a map for the specified key(s) as a map[string]uint64.

func (*JSON) MustGetUint8 added in v1.2.9

func (j *JSON) MustGetUint8(keys ...interface{}) (ret uint8, e error)

MustGetUint8 will return the value for the specified key(s) as a uint8.

func (*JSON) MustGetUint8Array added in v1.2.9

func (j *JSON) MustGetUint8Array(keys ...interface{}) (ret []uint8, e error)

MustGetUint8Array will return an array for the specified key(s) as a []uint8.

func (*JSON) MustGetUint8Map added in v1.2.9

func (j *JSON) MustGetUint8Map(keys ...interface{}) (ret map[string]uint8, e error)

MustGetUint8Map will return a map for the specified key(s) as a map[string]uint8.

func (*JSON) MustGetUintArray added in v1.2.0

func (j *JSON) MustGetUintArray(keys ...interface{}) (ret []uint, e error)

MustGetUintArray will return an array for the specified key(s) as a []uint.

func (*JSON) MustGetUintMap added in v1.2.0

func (j *JSON) MustGetUintMap(keys ...interface{}) (ret map[string]uint, e error)

MustGetUintMap will return a map for the specified key(s) as a map[string]uint.

func (*JSON) Set

func (j *JSON) Set(value interface{}, keys ...interface{}) error

Set will set the specified value for the specified key in the JSON blob.

func (*JSON) SetBlob added in v1.0.1

func (j *JSON) SetBlob(blob ...string) error

SetBlob will replace the underlying map[string]interface{} with a new JSON blob.

func (*JSON) SetEscapeHTML added in v1.0.6

func (j *JSON) SetEscapeHTML(escape bool)

SetEscapeHTML will set whether or not Marshalling should escape HTML special characters.

func (*JSON) String added in v1.1.4

func (j *JSON) String() (ret string)

String will return a string representation of JSON instance.

Jump to

Keyboard shortcuts

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