thedevsaddam / gojsonq

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

FromInterface custom decoder #67

Closed Dragomir-Ivanov closed 4 years ago

Dragomir-Ivanov commented 4 years ago

Hi there, Is there any way to load JSON document from map[string]interface{}? Simplest use case is querying MongoDB, which returns result documents in that format(without using schema struct). I am willing to make a PR if little help is supplied. Thanks

Dragomir-Ivanov commented 4 years ago

Hi again, Here is one naive implementation of the said feature:

func (j *JSONQ) FromInterface(v interface{}) *JSONQ {
    if m, ok := v.(map[string]interface{}); ok {
        j.rootJSONContent = m
        j.jsonContent = j.rootJSONContent
        return j
    }
    return j.addError(fmt.Errorf("%s is not map[string]interface{}", v))
}

If you are willing to merge this, I will make a proper PR with test cases added. Thanks. I really need this.

thedevsaddam commented 4 years ago

This is available from v2.5.0

Dragomir-Ivanov commented 4 years ago

Thanks a ton!