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

Multiple nested array support #33

Closed johndww closed 5 years ago

johndww commented 5 years ago

Is it possible to traverse multiple nested arrays in this library without knowing the array indexes? I have a scenario that works well with where clauses on the first array, but I haven't had any success with a second array below that.

This is a successful query using Find (but assumes I know the index to each array):

Find("data.items.[1].nestedA.[3].nestedB.value")

I've tried many variations of: From("data.items").Where("itemValue", "=", "asdf").From("nestedA").Where("nestedBOtherValue", "eq", "fdsa").Find("nestedB.value") without any luck.

thedevsaddam commented 5 years ago

@johndww Can you provide a JSON document related to the query you want?

johndww commented 5 years ago
{
  "arrayOuter": [
    {
      "fieldInner": "123",
      "arrayInner": [
        {
          "field1": "abc",
          "field2": "mike1"
        },
        {
          "field1": "def",
          "field2": "mike2"
        }
      ]
    },
    {
      "fieldInner": "456",
      "arrayInner": [
        {
          "field1": "abc",
          "field2": "mike3"
        },
        {
          "field1": "def",
          "field2": "mike4"
        }
      ]
    }
  ]
}

Find("arrayOuter.[1].arrayInner.[1].field2") returns me "mike4". I would like to programmatically do that with filters on fieldInner = 456 and field1 = "def"

thedevsaddam commented 5 years ago

Can you check the code below (Use dev branch):


package main

import (
    "github.com/davecgh/go-spew/spew"
    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().File("./data.json").From("arrayOuter")
    jq.WhereEqual("fieldInner", "456")
    jq.More(). //You have a collection of nodes where you can find any item from any node
            From("[0].arrayInner").WhereEqual("field1", "def")
    spew.Dump(jq.Get(), jq.Error())
}
johndww commented 5 years ago

Edit: sorry I missed the 'dev' branch part. Trying that out

@thedevsaddam Thanks for the response, I can't find the method More() anywhere. I don't see it in the doc https://github.com/thedevsaddam/gojsonq/wiki/Queries either.

johndww commented 5 years ago

@thedevsaddam the More() function is fantastic, that's exactly what I need!

thedevsaddam commented 5 years ago

@johndww Did not add documentation, it's in dev branch and testing mode. Hope you'll find it soon

johndww commented 5 years ago

Thanks @thedevsaddam , I look forward to it being merged here. Until then I'll use that branch.

thedevsaddam commented 5 years ago

@johndww Use v2.0 for More() method. See doc