gojq

package module
v0.0.0-...-df5c404 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: ISC Imports: 7 Imported by: 26

README

gojq

JSON query in Golang.

Install

go get -u github.com/elgs/gojq

This library serves three purposes:

  • makes parsing JSON configuration file much easier
  • enables JSON expression evaluation
  • reduces the pain of type assertion parsing JSON

Query from JSON Object

package main

import (
	"fmt"

	"github.com/elgs/gojq"
)

var jsonObj = `
{
  "name": "sam",
  "gender": "m",
  "pet": null,
  "skills": [
    "Eating",
    "Sleeping",
    "Crawling"
  ],
  "hello.world":true
}
`

func main() {
	parser, err := gojq.NewStringQuery(jsonObj)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(parser.Query("name"))          // sam <nil>
	fmt.Println(parser.Query("gender"))        // m <nil>
	fmt.Println(parser.Query("skills.[1]"))    // Sleeping <nil>
	fmt.Println(parser.Query("hello"))         // <nil> hello does not exist.
	fmt.Println(parser.Query("pet"))           // <nil> <nil>
	fmt.Println(parser.Query("."))             // map[name:sam gender:m pet:<nil> skills:[Eating Sleeping Crawling] hello.world:true] <nil>
	fmt.Println(parser.Query("'hello.world'")) // true <nil>
}

Query from JSON Array

package main

import (
	"fmt"
	"github.com/elgs/gojq"
)

var jsonArray = `
[
  {
    "name": "elgs",
    "gender": "m",
    "skills": [
      "Golang",
      "Java",
      "C"
    ]
  },
  {
    "name": "enny",
    "gender": "f",
    "skills": [
      "IC",
      "Electric design",
      "Verification"
    ]
  },
  {
    "name": "sam",
    "gender": "m",
	"pet": null,
    "skills": [
      "Eating",
      "Sleeping",
      "Crawling"
    ]
  }
]
`

func main() {
	parser, err := gojq.NewStringQuery(jsonArray)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(parser.Query("[0].name"))       // elgs <nil>
	fmt.Println(parser.Query("[1].gender"))     // f <nil>
	fmt.Println(parser.Query("[2].skills.[1]")) // Sleeping <nil>
	fmt.Println(parser.Query("[2].hello"))      // <nil> hello does not exist.
	fmt.Println(parser.Query("[2].pet"))        // <nil> <nil>
}

Nested Query

package main

import (
	"fmt"
	"github.com/elgs/gojq"
)

var jsonArray = `
[
  {
    "name": "elgs",
    "gender": "m",
    "skills": [
      "Golang",
      "Java",
      "C"
    ]
  },
  {
    "name": "enny",
    "gender": "f",
    "skills": [
      "IC",
      "Electric design",
      "Verification"
    ]
  },
  {
    "name": "sam",
    "gender": "m",
	"pet": null,
    "skills": [
      "Eating",
      "Sleeping",
      "Crawling"
    ]
  }
]
`

func main() {
	parser, err := gojq.NewStringQuery(jsonArray)
	if err != nil {
		fmt.Println(err)
		return
	}
	samSkills, err := parser.Query("[2].skills")
	fmt.Println(samSkills, err) //[Eating Sleeping Crawling] <nil>
	samSkillParser := gojq.NewQuery(samSkills)
	fmt.Println(samSkillParser.Query("[1]")) //Sleeping <nil>
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JQ

type JQ struct {
	Data any
}

JQ (JSON Query) struct

func NewFileQuery

func NewFileQuery(jsonFile string) (*JQ, error)

NewFileQuery - Create a new &JQ from a JSON file.

func NewQuery

func NewQuery(jsonObject any) *JQ

NewQuery - Create a &JQ from an any parsed by json.Unmarshal

func NewStringQuery

func NewStringQuery(jsonString string) (*JQ, error)

NewStringQuery - Create a new &JQ from a raw JSON string.

func (*JQ) Query

func (jq *JQ) Query(exp string) (any, error)

Query queries against the JSON with the expression passed in. The exp is separated by dots (".")

func (*JQ) QueryToArray

func (jq *JQ) QueryToArray(exp string) ([]any, error)

QueryToMap queries against the JSON with the expression passed in, and convert to a array: []any

func (*JQ) QueryToBool

func (jq *JQ) QueryToBool(exp string) (bool, error)

QueryToMap queries against the JSON with the expression passed in, and convert to bool

func (*JQ) QueryToFloat64

func (jq *JQ) QueryToFloat64(exp string) (float64, error)

QueryToMap queries against the JSON with the expression passed in, and convert to float64

func (*JQ) QueryToInt64

func (jq *JQ) QueryToInt64(exp string) (int64, error)

QueryToMap queries against the JSON with the expression passed in, and convert to int64

func (*JQ) QueryToMap

func (jq *JQ) QueryToMap(exp string) (map[string]any, error)

QueryToMap queries against the JSON with the expression passed in, and convert to a map[string]any

func (*JQ) QueryToString

func (jq *JQ) QueryToString(exp string) (string, error)

QueryToMap queries against the JSON with the expression passed in, and convert to string

Jump to

Keyboard shortcuts

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