thedevsaddam / gojsonq

A simple Go package to Query over JSON/YAML/XML/CSV Data
https://github.com/thedevsaddam/gojsonq/wiki
MIT License
2.18k stars 140 forks source link

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

Closed dionysius closed 5 years ago

dionysius commented 5 years ago

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?

dionysius commented 5 years ago

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`))