php-jsonq
JsonQ is a simple, elegant PHP package to Query over any type of JSON Data. It'll make your life easier by giving the flavour of an ORM-like query on your JSON., (*1)
Support For This Project
Hey due, please help me out for daily improve this project, (*2)
, (*3)
NOTE
From the version JsonQ 6.0 all the features rewrite from QAarray. After a long run we noticed that Query Engine of JsonQ should be seperate. Becouse people want to query over various types of data like CSV, YAML, XML. So if we keep the query engine tightly coupled with this project its make no sense. Thats why we move the Query Engine part and develop a new package QAarray. Qarray is designed for queryng over native PHP array and anyone can implement their own Engine, like JsonQ., (*4)
Please do not update to >= 6.0 version directly from bellow
Installation
composer require nahid/jsonq
Usage
You can start using this package right away by importing your JSON data from a file:, (*5)
use Nahid/JsonQ/Jsonq;
$jsonq = new Jsonq('data.json');
Or from a JSON String:, (*6)
$json->json('{"id": 1, "name": "Nahid"}');
Or from a PHP Array:, (*7)
$json->collect(['id'=>1, 'name'=>'Nahid']);
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., (*8)
Let's see a quick example:, (*9)
//data.json
{
"name": "products",
"description": "Features product list",
"vendor":{
"name": "Computer Source BD",
"email": "info@example.com",
"website":"www.example.com"
},
"users":[
{"id":1, "name":"Johura Akter Sumi", "location": "Barisal"},
{"id":2, "name":"Mehedi Hasan Nahid", "location": "Barisal"},
{"id":3, "name":"Ariful Islam", "location": "Barisal"},
{"id":4, "name":"Suhel Ahmed", "location": "Sylhet"},
{"id":5, "name":"Firoz Serniabat", "location": "Gournodi"},
{"id":6, "name":"Musa Jewel", "location": "Barisal", "visits": [
{"name": "Sylhet", "year": 2011},
{"name": "Cox's Bazar", "year": 2012},
{"name": "Bandarbar", "year": 2014}
]}
],
"products": [
{"id":1, "user_id": 2, "city": "bsl", "name":"iPhone", "cat":1, "price": 80000},
{"id":2, "user_id": 2, "city": null, "name":"macbook pro", "cat": 2, "price": 150000},
{"id":3, "user_id": 2, "city": "dhk", "name":"Redmi 3S Prime", "cat": 1, "price": 12000},
{"id":4, "user_id": 1, "city": null, "name":"Redmi 4X", "cat":1, "price": 15000},
{"id":5, "user_id": 1, "city": "bsl", "name":"macbook air", "cat": 2, "price": 110000},
{"id":6, "user_id": 2, "city": null, "name":"macbook air 1", "cat": 2, "price": 81000}
]
}
use Nahid\JsonQ\Jsonq;
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 2)
->get();
dump($res);
//This will print
/*
array:3 [▼
1 => {#7 ▼
+"id": 2
+"user_id": 2
+"city": null
+"name": "macbook pro"
+"cat": 2
+"price": 150000
}
4 => {#8 ▼
+"id": 5
+"user_id": 1
+"city": "bsl"
+"name": "macbook air"
+"cat": 2
+"price": 110000
}
5 => {#9 ▼
+"id": 6
+"user_id": 2
+"city": null
+"name": "macbook air 1"
+"cat": 2
+"price": 81000
}
]
*/
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():, (*10)
$result = $json->from('products')
->where('cat', '=', 2)
->sum('price');
dump($result);
//It will print:
/*
365000
*/
Pretty neat, huh?, (*11)
Let's explore the full API to see what else magic this library can do for you.
Shall we?, (*12)
API
Following API examples are shown based on the sample JSON data given here. To get a better idea of the examples see that JSON data first. Also detailed examples of each API can be found here., (*13)
List of API:, (*14)
fetch()
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., (*15)
find(path)
-
path
-- the path hierarchy of the data you want to find.
You don't need to call fetch()
method after this. Because this method will fetch and return the data by itself., (*16)
caveat: You can't chain further query methods after it. If you need that, you should use at()
or from()
method., (*17)
example:, (*18)
Let's say you want to get the value of 'cities' property of your Json Data. You can do it like this:, (*19)
$q = new Jsonq('data.json');
echo $q->find('vendor.name');
If you want to traverse to more deep in hierarchy, you can do it like:, (*20)
$q = new Jsonq('data.json');
echo $q->find('vendor.name');
See a detail example here., (*21)
from(path)
-
path
(optional) -- the path hierarchy of the data you want to start query from.
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., (*22)
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., (*23)
example:, (*24)
Let's say you want to start query over the values of 'vendor.name' property of your JSON Data. You can do it like this:, (*25)
$q = new Jsonq('data.json');
echo $q->from('vendor.name')->get();
If you want to traverse to more deep in hierarchy, you can do it like:, (*26)
$q = new Jsonq('data.json');
echo $q->from('users.5.visits')->get();
See a detail example here., (*27)
at(path)
This is an alias method of from()
and will behave exactly like that. See example here., (*28)
where(key, condition, val)
example:, (*30)
Let's say you want to find the 'users' who has id
of 1
. You can do it like this:, (*31)
$q = new Jsonq('data.json');
$res = $q->from('users')->where('id', '=', 1)->get();
You can add multiple where conditions. It'll give the result by AND-ing between these multiple where conditions., (*32)
$q = new Jsonq('data.json');
$res = $q->from('users')
->where('id', '=', 1)
->where('location', '=', 'barisal')
->get();
See a detail example here., (*33)
orWhere(key, op, val)
Parameters of orWhere()
are the same as where()
. The only difference between where()
and orWhere()
is: condition given by the orWhere()
method will OR-ed the result with other conditions., (*34)
For example, if you want to find the users with id of 1
or 2
, you can do it like this:, (*35)
$q = new Jsonq('data.json');
$res = $q->from('users')
->where('id', '=', 1)
->orWhere('id', '=', 2)
->get();
See detail example here., (*36)
whereIn(key, val)
-
key
-- the property name of the data
-
val
-- it should be an Array
This method will behave like where(key, 'in', val)
method call., (*37)
whereNotIn(key, val)
-
key
-- the property name of the data
-
val
-- it should be an Array
This method will behave like where(key, 'notin', val)
method call., (*38)
whereNull(key)
-
key
-- the property name of the data
This method will behave like where(key, 'null')
or where(key, '=', null)
method call., (*39)
whereNotNull(key)
-
key
-- the property name of the data
This method will behave like where(key, 'notnull')
or where(key, '!=', null)
method call., (*40)
whereStartsWith(key, val)
-
key
-- the property name of the data
-
val
-- it should be a String
This method will behave like where(key, 'startswith', val)
method call., (*41)
whereEndsWith(key, val)
-
key
-- the property name of the data
-
val
-- it should be a String
This method will behave like where(key, 'endswith', val)
method call., (*42)
whereContains(key, val)
-
key
-- the property name of the data
-
val
-- it should be a String
This method will behave like where(key, 'contains', val)
method call., (*43)
sum(column)
-
column
-- the property name of the data
example:, (*44)
Let's say you want to find the sum of the 'price' of the 'products'. You can do it like this:, (*45)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->sum('price');
If the data you are aggregating is plain array, you don't need to pass the 'column' parameter.
See detail example here, (*46)
count()
It will return the number of elements in the collection., (*47)
example:, (*48)
Let's say you want to find how many elements are in the 'products' property. You can do it like:, (*49)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->count();
See detail example here., (*50)
size()
This is an alias method of count()
., (*51)
max(column)
-
column
-- the property name of the data
example:, (*52)
Let's say you want to find the maximum of the 'price' of the 'products'. You can do it like this:, (*53)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->max('price);
If the data you are querying is plain array, you don't need to pass the 'column' parameter.
See detail example here, (*54)
min(column)
-
column
-- the property name of the data
example:, (*55)
Let's say you want to find the minimum of the 'price' of the 'products'. You can do it like this:, (*56)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->min('price');
If the data you are querying is plain array, you don't need to pass the 'property' parameter.
See detail example here, (*57)
avg(column)
-
column
-- the property name of the data
example:, (*58)
Let's say you want to find the average of the 'price' of the 'products'. You can do it like this:, (*59)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->avg('price');
If the data you are querying is plain array, you don't need to pass the 'column' parameter.
See detail example here, (*60)
first()
It will return the first element of the collection., (*61)
example:, (*62)
$q = new jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->first();
See detail example here., (*63)
last()
It will return the last element of the collection., (*64)
example:, (*65)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->last();
See detail example here., (*66)
nth(index)
-
index
-- index of the element to be returned.
It will return the nth element of the collection. If the given index is a positive value, it will return the nth element from the beginning. If the given index is a negative value, it will return the nth element from the end., (*67)
example:, (*68)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->nth(2);
See detail example here., (*69)
exists()
It will return true if the element is not empty or not null or not an empty array or not an empty object., (*70)
example:, (*71)
Let's say you want to find how many elements are in the 'products' property. You can do it like:, (*72)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->exists();
See detail example here., (*73)
groupBy(column)
-
column
-- The property by which you want to group the collection.
example:, (*74)
Let's say you want to group the 'users' data based on the 'location' property. You can do it like:, (*75)
$q = new Jsonq('data.json');
$res = $q->from('users')
->groupBy('location')
->get();
See detail example here., (*76)
sort(order)
-
order
-- If you skip the 'order' property the data will be by default ordered as ascending. You need to pass 'desc' as the 'order' parameter to sort the data in descending order. Also, you can pass a compare function in 'order' parameter to define your own logic to order the data.
Note: This method should be used for plain Array. If you want to sort an Array of Objects you should use the sortBy() method described later., (*77)
example:, (*78)
Let's say you want to sort the 'arr' data. You can do it like:, (*79)
$q = new Jsonq();
$res = $q->collect([7, 5, 9, 1, 3])
->sort();
See detail example here., (*80)
sortBy(column, order)
-
column
-- You need to pass the column name on which the sorting will be done.
-
order
-- If you skip the 'order' property the data will be by default ordered as ascending. You need to pass 'desc' as the 'order' parameter to sort the data in descending order. Also, you can pass a compare function in 'order' parameter to define your own logic to order the data.
Note: This method should be used for Array of Objects. If you want to sort a plain Array you should use the sort() method described earlier., (*81)
example:, (*82)
Let's say you want to sort the 'price' data of 'products'. You can do it like:, (*83)
$q = new Jsonq('data.json');
$res = $q->from('products')
->where('cat', '=', 1)
->sortBy('price', 'desc');
See detail example here., (*84)
reset(data)
-
data
-- can be a JSON file path, or a JSON string or a JSON Object. If no data passed in the data
parameter, the jsonQ
Object instance will be reset to previously initialized data.
At any point, you might want to reset the Object instance to a completely different set of data and then query over it. You can use this method in that case., (*85)
See a detail example here., (*86)
copy()
It will return a complete clone of the Object instance., (*87)
See a detail example here., (*88)
Bugs and Issues
If you encounter any bugs or issues, feel free to open an issue at
github., (*89)
Also, you can shoot me an email to
nahid.dns@gmail.com for hugs or bugs., (*90)
This package has also different language support., (*91)
Support on Beerpay
Hey dude! Help me out for a couple of :beers:!, (*92)
, (*93)