Skip to content

Commit bc153ee

Browse files
committed
Initial commit
0 parents  commit bc153ee

12 files changed

+3077
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data.json
2+
.idea
3+
.DS_Store

.travis.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: go
2+
sudo: false
3+
4+
matrix:
5+
include:
6+
- go: 1.4
7+
- go: 1.5
8+
- go: 1.6
9+
- go: 1.7
10+
- go: 1.8
11+
- go: 1.9
12+
- go: tip
13+
allow_failures:
14+
- go: tip
15+
before_install:
16+
- go get github.com/mattn/goveralls
17+
script:
18+
- $GOPATH/bin/goveralls -service=travis-ci
19+
- go get -t -v ./...
20+
- diff -u <(echo -n) <(gofmt -d .)
21+
- go vet $(go list ./... | grep -v /vendor/)
22+
- go test -v -race ./...

CONTRIBUTING.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Contributing
2+
3+
## Must follow the guide for issues
4+
- Use the search tool before opening a new issue.
5+
- Please provide source code and stack trace if you found a bug.
6+
- Please review the existing issues and then provide feedback
7+
8+
## Pull Request Process
9+
- You MUST send pull requests against `dev` branch
10+
- It should pass all tests in the available continuous integrations systems such as TravisCI.
11+
- You should add/modify tests to cover your proposed code changes.
12+
- If your pull request contains a new feature, please document it on the README.

LICENSE.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
CC0 1.0 Universal
2+
3+
Statement of Purpose
4+
5+
The laws of most jurisdictions throughout the world automatically confer
6+
exclusive Copyright and Related Rights (defined below) upon the creator and
7+
subsequent owner(s) (each and all, an "owner") of an original work of
8+
authorship and/or a database (each, a "Work").
9+
10+
Certain owners wish to permanently relinquish those rights to a Work for the
11+
purpose of contributing to a commons of creative, cultural and scientific
12+
works ("Commons") that the public can reliably and without fear of later
13+
claims of infringement build upon, modify, incorporate in other works, reuse
14+
and redistribute as freely as possible in any form whatsoever and for any
15+
purposes, including without limitation commercial purposes. These owners may
16+
contribute to the Commons to promote the ideal of a free culture and the
17+
further production of creative, cultural and scientific works, or to gain
18+
reputation or greater distribution for their Work in part through the use and
19+
efforts of others.
20+
21+
For these and/or other purposes and motivations, and without any expectation
22+
of additional consideration or compensation, the person associating CC0 with a
23+
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
24+
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
25+
and publicly distribute the Work under its terms, with knowledge of his or her
26+
Copyright and Related Rights in the Work and the meaning and intended legal
27+
effect of CC0 on those rights.
28+
29+
1. Copyright and Related Rights. A Work made available under CC0 may be
30+
protected by copyright and related or neighboring rights ("Copyright and
31+
Related Rights"). Copyright and Related Rights include, but are not limited
32+
to, the following:
33+
34+
i. the right to reproduce, adapt, distribute, perform, display, communicate,
35+
and translate a Work;
36+
37+
ii. moral rights retained by the original author(s) and/or performer(s);
38+
39+
iii. publicity and privacy rights pertaining to a person's image or likeness
40+
depicted in a Work;
41+
42+
iv. rights protecting against unfair competition in regards to a Work,
43+
subject to the limitations in paragraph 4(a), below;
44+
45+
v. rights protecting the extraction, dissemination, use and reuse of data in
46+
a Work;
47+
48+
vi. database rights (such as those arising under Directive 96/9/EC of the
49+
European Parliament and of the Council of 11 March 1996 on the legal
50+
protection of databases, and under any national implementation thereof,
51+
including any amended or successor version of such directive); and
52+
53+
vii. other similar, equivalent or corresponding rights throughout the world
54+
based on applicable law or treaty, and any national implementations thereof.
55+
56+
2. Waiver. To the greatest extent permitted by, but not in contravention of,
57+
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
58+
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
59+
and Related Rights and associated claims and causes of action, whether now
60+
known or unknown (including existing as well as future claims and causes of
61+
action), in the Work (i) in all territories worldwide, (ii) for the maximum
62+
duration provided by applicable law or treaty (including future time
63+
extensions), (iii) in any current or future medium and for any number of
64+
copies, and (iv) for any purpose whatsoever, including without limitation
65+
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
66+
the Waiver for the benefit of each member of the public at large and to the
67+
detriment of Affirmer's heirs and successors, fully intending that such Waiver
68+
shall not be subject to revocation, rescission, cancellation, termination, or
69+
any other legal or equitable action to disrupt the quiet enjoyment of the Work
70+
by the public as contemplated by Affirmer's express Statement of Purpose.
71+
72+
3. Public License Fallback. Should any part of the Waiver for any reason be
73+
judged legally invalid or ineffective under applicable law, then the Waiver
74+
shall be preserved to the maximum extent permitted taking into account
75+
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
76+
is so judged Affirmer hereby grants to each affected person a royalty-free,
77+
non transferable, non sublicensable, non exclusive, irrevocable and
78+
unconditional license to exercise Affirmer's Copyright and Related Rights in
79+
the Work (i) in all territories worldwide, (ii) for the maximum duration
80+
provided by applicable law or treaty (including future time extensions), (iii)
81+
in any current or future medium and for any number of copies, and (iv) for any
82+
purpose whatsoever, including without limitation commercial, advertising or
83+
promotional purposes (the "License"). The License shall be deemed effective as
84+
of the date CC0 was applied by Affirmer to the Work. Should any part of the
85+
License for any reason be judged legally invalid or ineffective under
86+
applicable law, such partial invalidity or ineffectiveness shall not
87+
invalidate the remainder of the License, and in such case Affirmer hereby
88+
affirms that he or she will not (i) exercise any of his or her remaining
89+
Copyright and Related Rights in the Work or (ii) assert any associated claims
90+
and causes of action with respect to the Work, in either case contrary to
91+
Affirmer's express Statement of Purpose.
92+
93+
4. Limitations and Disclaimers.
94+
95+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
96+
surrendered, licensed or otherwise affected by this document.
97+
98+
b. Affirmer offers the Work as-is and makes no representations or warranties
99+
of any kind concerning the Work, express, implied, statutory or otherwise,
100+
including without limitation warranties of title, merchantability, fitness
101+
for a particular purpose, non infringement, or the absence of latent or
102+
other defects, accuracy, or the present or absence of errors, whether or not
103+
discoverable, all to the greatest extent permissible under applicable law.
104+
105+
c. Affirmer disclaims responsibility for clearing rights of other persons
106+
that may apply to the Work or any use thereof, including without limitation
107+
any person's Copyright and Related Rights in the Work. Further, Affirmer
108+
disclaims responsibility for obtaining any necessary consents, permissions
109+
or other rights required for any use of the Work.
110+
111+
d. Affirmer understands and acknowledges that Creative Commons is not a
112+
party to this document and has no duty or obligation with respect to this
113+
CC0 or use of the Work.
114+
115+
For more information, please see
116+
<http://creativecommons.org/publicdomain/zero/1.0/>

README.md

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
gojsonq
2+
===============
3+
[![Build Status](https://travis-ci.org/thedevsaddam/gojsonq.svg?branch=master)](https://travis-ci.org/thedevsaddam/gojsonq)
4+
[![Project status](https://img.shields.io/badge/version-beta-yellow.svg)](https://github.com/thedevsaddam/gojsonq/releases)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/thedevsaddam/gojsonq)](https://goreportcard.com/report/github.com/thedevsaddam/gojsonq)
6+
[![Coverage Status](https://coveralls.io/repos/github/thedevsaddam/gojsonq/badge.svg?branch=master)](https://coveralls.io/github/thedevsaddam/gojsonq?branch=master)
7+
[![GoDoc](https://godoc.org/github.com/thedevsaddam/gojsonq?status.svg)](https://godoc.org/github.com/thedevsaddam/gojsonq)
8+
[![License: CC0-1.0](https://img.shields.io/badge/License-CC0%201.0-lightgrey.svg)](https://github.com/thedevsaddam/gojsonq/blob/dev/LICENSE.md)
9+
10+
A simple Go package to Query over JSON Data
11+
12+
13+
### Installation
14+
15+
Install the package using
16+
```go
17+
$ go get github.com/thedevsaddam/gojsonq
18+
```
19+
20+
### Usage
21+
22+
To use the package import it in your `*.go` code
23+
```go
24+
import "github.com/thedevsaddam/gojsonq"
25+
```
26+
27+
28+
You can start Query your data using the various query methods such as **Find**, **Where**, **OrWhere**, **WhereIn**, **WhereStartsWith**, **WhereEndsWith**, **WhereContains** and so on. Also you can aggregate your data after query using **Sum**, **Count**, **GroupBy**, **Max**, **Min** etc.
29+
30+
Let's see a quick example:
31+
32+
<details><summary>Sample data (data.json)</summary>
33+
```json
34+
{
35+
"name":"computers",
36+
"description":"List of computer products",
37+
"prices":[2400, 2100, 1200, 400.87, 89.90, 150.10],
38+
"names":["John Doe", "Jane Doe", "Tom", "Jerry", "Nicolas", "Abby"],
39+
"items":[
40+
{
41+
"id":1,
42+
"name":"MacBook Pro 13 inch retina",
43+
"price":1350
44+
},
45+
{
46+
"id":2,
47+
"name":"MacBook Pro 15 inch retina",
48+
"price":1700
49+
},
50+
{
51+
"id":3,
52+
"name":"Sony VAIO",
53+
"price":1200
54+
},
55+
{
56+
"id":4,
57+
"name":"Fujitsu",
58+
"price":850
59+
},
60+
{
61+
"id":null,
62+
"name":"HP core i3 SSD",
63+
"price":850
64+
}
65+
]
66+
}
67+
```
68+
</details>
69+
70+
**Example:**
71+
```go
72+
jq := gojsonq.New().
73+
File("./data.json").
74+
From("items").
75+
Where("price", ">", 1200)
76+
fmt.Printf("%#v\n", jq.Get())
77+
```
78+
**Output:**
79+
```go
80+
[]interface {}{
81+
map[string]interface {}{"id":1, "name":"MacBook Pro 13 inch retina", "price":1350},
82+
map[string]interface {}{"id":2, "name":"MacBook Pro 15 inch retina", "price":1700},
83+
}
84+
```
85+
86+
Let's say we want to get the Summation of price of the Queried result. We can do it easily by calling the Sum() method instead of Get():
87+
88+
**Example**
89+
```go
90+
jq := gojsonq.New().
91+
File("./data.json").
92+
From("items").
93+
Where("price", ">", 1200)
94+
fmt.Printf("%#v\n", jq.Get())
95+
```
96+
97+
**Output**
98+
```json
99+
(float64) 3050
100+
```
101+
102+
103+
Let's explore the full API to see what else magic this library can do for you.
104+
Shall we?
105+
106+
## API
107+
108+
Following API examples are shown based on the sample JSON data given above. To get a better idea of the examples see that JSON data first.
109+
110+
**List of API:**
111+
112+
* [File](#file)
113+
* [JSONString](#jsonstring)
114+
* [Reader](#reader)
115+
* [Get](#get)
116+
* [Find](#find)
117+
* [From](#from)
118+
* [Where](#wherekey-op-val)
119+
* [OrWhere](#orwherekey-op-val)
120+
* [WhereIn](#whereinkey-val)
121+
* [WhereNotIn](#wherenotinkey-val)
122+
* [WhereNil](#wherenilkey)
123+
* [WhereNotNil](#wherenotnilkey)
124+
* [WhereEqual](#whereequalkey)
125+
* [WhereNotEqual](#wherenotequalkey)
126+
* [WhereStartsWith](#wherestartswithkey-val)
127+
* [WhereEndsWith](#whereendswithkey-val)
128+
* [WhereContains](#wherecontainskey-val)
129+
* [WhereStrictContains](#wherestrictcontainskey-val)
130+
* [Sum](#sumproperty)
131+
* [Count](#count)
132+
* [Max](#maxproperty)
133+
* [Min](#minproperty)
134+
* [Avg](#avgproperty)
135+
* [First](#first)
136+
* [Last](#last)
137+
* [Nth](#nthindex)
138+
* [GroupBy](#groupbyproperty)
139+
* [Sort](#sortorder)
140+
* [Reset](#resetdata)
141+
* [Only](#only)
142+
* [Pluck](#pluck)
143+
144+
### `Get()`
145+
146+
This method will execute queries and will return the resulted data. You need to call it finally after using some query methods. Details can be found in other API examples.
147+
148+
### `Find(path)`
149+
150+
* `path` -- the path hierarchy of the data you want to find.
151+
152+
You don't need to call `Get()` method after this. Because this method will fetch and return the data by itself.
153+
154+
**caveat:** You can't chain further query methods after it. If you need that, you should use `From()` method.
155+
156+
**example:**
157+
158+
Let's say you want to get the value of _'items'_ property of your JSON Data. You can do it like this:
159+
160+
```go
161+
items := gojsonq.New().File("./data.json").Find("vendor.items");
162+
fmt.Printf("%#v\n", items)
163+
```
164+
165+
If you want to traverse to more deep in hierarchy, you can do it like:
166+
167+
```go
168+
item := gojsonq.New().File("./data.json").Find("vendor.items.[0]");
169+
fmt.Printf("%#v\n", item)
170+
```
171+
172+
### `From(path)`
173+
174+
* `path` (optional) -- the path hierarchy of the data you want to start query from.
175+
176+
By default, query would be started from the root of the JSON Data you've given. If you want to first move to a nested path hierarchy of the data from where you want to start your query, you would use this method. Skipping the `path` parameter or giving **'.'** as parameter will also start query from the root Data.
177+
178+
Difference between this method and `Find()` is that, `Find()` method will return the data from the given path hierarchy. On the other hand, this method will return the Object instance, so that you can further chain query methods after it.
179+
180+
**Example:**
181+
182+
Let's say you want to start query over the values of _'items'_ property of your Json Data. You can do it like this:
183+
184+
```go
185+
jq := gojsonq.New().File("./data.json").From("items").Where("price", ">", 1200)
186+
fmt.Printf("%#v\n", jq.Get())
187+
```
188+
189+
If you want to traverse to more deep in hierarchy, you can do it like:
190+
191+
```go
192+
jq := gojsonq.New().File("./data.json").From("vendor.items").Where("price", ">", 1200)
193+
fmt.Printf("%#v\n", jq.Get())
194+
```
195+
## TODO
196+
197+
- [ ] Add missing methods
198+
- [ ] Write full documentation with example
199+
200+
## Bugs and Issues
201+
202+
If you encounter any bugs or issues, feel free to [open an issue at
203+
github](https://github.com/thedevsaddam/gojsonq/issues).
204+
205+
Also, you can shoot me an email to
206+
<mailto:thedevsaddam@gmail.com> for hugs or bugs.
207+
208+
## Credit
209+
210+
Speical thanks to [Nahid Bin Azhar](https://github.com/nahid) for the inspiration and guidance for the package.
211+
212+
## Contribution
213+
If you are interested to make the package better please send pull requests or create an issue so that others can fix.
214+
[Read the contribution guide here](CONTRIBUTING.md)
215+
216+
## See all [contributors](https://github.com/thedevsaddam/gojsonq/graphs/contributors)

0 commit comments

Comments
 (0)