thedevsaddam / gojsonq

A simple Go package for querying over JSON, YAML, XML, and CSV data.

Home Page:https://github.com/thedevsaddam/gojsonq/wiki

Repository from Github https://github.comthedevsaddam/gojsonqRepository from Github https://github.comthedevsaddam/gojsonq

How do I Find() or Get() from which key has dots?

dionysius opened this issue · comments

Hi, I have the following issue:

package main

import (
	"fmt"

	"github.com/thedevsaddam/gojsonq"
)

var (
	jsonstring = `
		{
			"labels": {
				"foo": "bar",
				"io.kubernetes.pod.name": "mytest-sandbox",
				"io.kubernetes.pod.namespace": "default",
				"io.kubernetes.pod.uid": "c27e82eb-785a-4532-8121-e0bb0f8f2461"
			}
		}`
)

func main() {
	jq := gojsonq.New().JSONString(jsonstring)
	fmt.Println(jq.Find(`labels.foo`))
	fmt.Println(jq.Find(`labels.io.kubernetes.pod.name`))
	fmt.Println(jq.Find(`labels."io.kubernetes.pod.name"`))
	fmt.Println(jq.Find(`labels.[io.kubernetes.pod.name]`))
	fmt.Println(jq.Find(`labels.["io.kubernetes.pod.name"]`))
	fmt.Println(jq.Find(`labels[io.kubernetes.pod.name]`))
	fmt.Println(jq.Find(`labels["io.kubernetes.pod.name"]`))
	fmt.Println(jq.From("labels").Select("io.kubernetes.pod.name").Get())
}

which outputs:

bar
<nil>
<nil>
<nil>
<nil>
<nil>
<nil>
[]

Any idea what I could do to be able to select the value of a key with dots?

Aaaaah, Found it :)

go mod edit -require github.com/thedevsaddam/gojsonq@v2.0.1

which then allows me to do:

	jq = gojsonq.New(gojsonq.SetSeparator("->")).JSONString(jsonstring)
	fmt.Println(jq.Find(`labels->io.kubernetes.pod.name`))