jq
jq
is a jq-like library.
It's intended to simplify working with unstructured data.
It's aimed to be almost fully compatible with original jq.
It doesn't use reflection or usafe hacks.
It reuses memory -> less allocs -> less gc pressure -> better performance and less memory and cpu usage.
Usage
Data Reshaping
Code is from examples.
input := `{
"data": {
"entries": [
{"data": {"the actual interesting object": "here"}},
{"data": {"the actual interesting object": "here"}},
{"data": {"the actual interesting object": "here"}},
],
"pagination": {
"next_cursor": "some_cursor"
}
}
}`
f := jq.NewObject(
"results", jq.NewArray(
jq.NewQuery("data", "entries", jq.NewIter(), "data"),
),
"cursor", jq.NewQuery("data", "pagination", "next_cursor"),
)
s := jq.NewSandwich(
jqjson.NewDecoder(),
jqjson.NewEncoder(),
)
s.Reset()
r := strings.NewReader(input)
buf, err := io.ReadAll(r)
_ = err
var res []byte
res, err = s.ProcessAll(f, res[:0], buf)
_ = err
Format Conversion
query := "james bond"
input := `id,graphql_id,name,url,followers
124,ArWqv41,"James Bond","https://page-url.com/profile/124",100500
140,RefqWet,"James Bond Jr.","https://page-url.com/profile/140",1030
`
f := jq.NewPlus(jq.NewObject("query", jq.NewLiteral(query)), jq.Dot{})
d := jqcsv.NewDecoder()
d.Tag = cbor.Map
d.Header = true
e := jqjson.NewEncoder()
e.Separator = []byte{'\n'}
s := jq.NewSandwich(d, e)
s.Reset()
buf := []byte(input)
res, err := s.ProcessAll(f, nil, buf)
_ = err
_ = res
data := []byte(`{"key1":"eyJrZXkyIjoie1wia2V5M1wiOlwidmFsdWVcIn0ifQ=="}`)
f := jq.NewQuery(
"key1",
jqbase64.NewDecoder(base64.StdEncoding),
jqjson.NewDecoder(),
"key2",
jqjson.NewDecoder(),
"key3",
)
s := jq.NewSandwich(jqjson.NewDecoder(), nil)
s.Reset()
res, _, _, err := s.DecodeApply(f, data, 0)
_ = err
value, err := s.Buffer.Reader().BytesChecked(res)
_ = err
fmt.Printf("%s\n", value)