Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query json for array size. #22

Closed
lefscode opened this issue Jul 25, 2018 · 6 comments
Closed

Query json for array size. #22

lefscode opened this issue Jul 25, 2018 · 6 comments
Labels
discussion enhancement New feature or request priority-mid question Further information is requested

Comments

@lefscode
Copy link

lefscode commented Jul 25, 2018

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 thedevsaddam added question Further information is requested discussion labels Jul 25, 2018
@thedevsaddam
Copy link
Owner

Probably we should add this feature soon.

@thedevsaddam thedevsaddam added enhancement New feature or request priority-mid labels Jul 26, 2018
@lefscode
Copy link
Author

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
Copy link
Owner

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

@lefscode
Copy link
Author

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

@thedevsaddam
Copy link
Owner

thedevsaddam commented Jul 26, 2018

@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
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request priority-mid question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants