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

multilevel slice #23

Closed aliuygur closed 6 years ago

aliuygur commented 6 years ago
package main

import (
    "fmt"

    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().JSONString(jsonStr).From("users").Where("addresses.name", "=", "home")
    fmt.Printf("%+v%v", jq.Get(), jq.Error())

}

var jsonStr = `
{
   "users": [
       {
           "name": "jhon",
           "addresses": [
               {"name": "home"},
               {"name": "work"}
           ]
        }
   ]
}
`
➜  jsontest go run main.go
[]gojsonq: invalid node name name%

is there anyway to do it?

thedevsaddam commented 6 years ago

You can't match all the addresses.name for query, but you can specify one value from addresses array like:

package main

import (
    "fmt"

    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().JSONString(jsonStr).From("users").Where("addresses.[0].name", "=", "home")
    fmt.Printf("%+v%v", jq.Get(), jq.Error())

}

var jsonStr = `
{
   "users": [
       {
           "name": "jhon",
           "addresses": [
               {"name": "home"},
               {"name": "work"}
           ]
        }
   ]
}

But you may use Macro to achieve your requirements.

Here is an example of how to create custom macro: Custom Macro function