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

Query json for array size. #22

Closed lefscode closed 6 years ago

lefscode commented 6 years ago

Hello,

This is an "info request".

I have the following json and I would like to check if the input contains a father (fid) with two children.

{
  "data": [
    {
      "fid": 10,
      "children": [
        {
          "cid": 123
        },
        {
          "cid": 124
        }

      ]
    }
  ]
}

The code so far is

package main

import (
    "fmt"

    "github.com/thedevsaddam/gojsonq"
)

const json = `{
    "data": [
      {
        "fid": 10,
        "children": [
          {
            "cid": 123
          },
          {
            "cid": 124
          }

        ]
      }
    ]
  }`

func main() {

    q := gojsonq.New().JSONString(json).From("data").
        Where("fid", "=", 10)
    fmt.Println(q.Get())
}

How can I modify the source code to decide if fid=10 AND children.size() = 2?

In principle how can I use the "array size" to query the json file/input?

Thanks

thedevsaddam commented 6 years ago

Probably we should add this feature soon.

lefscode commented 6 years ago

Thanks for the follow up. I think that this would be very helpful .... From("data").Where("fid", "=", 10).Where("children" "OP", number).

Where OP could be count, contains, max, min, etc.... .... From("data").Where("fid", "=", 10).Where("children" "count", 2)

thedevsaddam commented 6 years ago

@lefscode For this time you can add custom Macro function to solve the problem

lefscode commented 6 years ago

@thedevsaddam, thanks. That's interesting. How can we get the existence of the "children" array and the capacity of this array?

thedevsaddam commented 6 years ago

@lefscode Here is a quick solution, please check errors and more types for your need.

package main

import (
    "fmt"

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

const json = `{
  "data":[
    {
      "fid":10,
      "children":[
        {
          "cid":121
        },
        {
          "cid":122
        },
        {
          "cid":125
        }
      ]
    },
    {
      "fid":10,
      "children":[
        {
          "cid":123
        },
        {
          "cid":124
        }
      ]
    }
  ]
}`

func main() {

    var count = func(x, y interface{}) (bool, error) {
        yv, ok := y.(int)
        if !ok {
            return false, fmt.Errorf("%v is must be an integer", y)
        }

        xv, ok := x.([]interface{}) // do various check for yourself
        if !ok {
            return false, fmt.Errorf("%v must be an array", x)
        }

        if len(xv) == yv {
            return true, nil
        }

        return false, nil
    }
    q := gojsonq.New().Macro("count", count).JSONString(json).From("data").
        Where("fid", "=", 10).Where("children", "count", 3)
    spew.Dump(q.Get(), q.Error())
}

As this type of task can be done by Macro I'm closing the issue.

thedevsaddam commented 6 years ago

@lefscode This is a good feature. You can use it from gojsonq v1.7.0

  1. https://github.com/thedevsaddam/gojsonq/wiki/Queries#wherelenequalkey-val

  2. https://github.com/thedevsaddam/gojsonq/wiki/Queries#wherekey-op-val