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.
Support For This Project
Hey due, please help me out for daily improve this project
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.
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:
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.
Let's explore the full API to see what else magic this library can do for you.
Shall we?
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.
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.
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.
caveat: You can't chain further query methods after it. If you need that, you should use at() or from() method.
example:
Let's say you want to get the value of 'cities' property of your Json Data. You can do it like this:
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.
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.
example:
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:
This is an alias method of from() and will behave exactly like that. See example here.
where(key, condition, val)
key -- the property name of the data. Or you can pass a Function here to group multiple query inside it. See details in example
val -- value to be matched with. It can be a int, string, bool or even Function - depending on the op.
op -- operand to be used for matching. The following operands are available to use:
= : For weak equality matching
eq : Same as =
!= : For weak not equality matching
neq : Same as !=
== : For strict equality matching
seq : Same as ==
!== : For strict not equality matching
sneq : Same as !==
> : Check if value of given key in data is Greater than val
gt : Same as >
< : Check if value of given key in data is Less than val
lt : Same as <
>= : Check if value of given key in data is Greater than or Equal of val
gte : Same as >=
<= : Check if value of given key in data is Less than or Equal of val
lte : Same as <=
null : Check if the value of given key in data is null (val parameter in where() can be omitted for this op)
notnull : Check if the value of given key in data is not null (val parameter in where() can be omitted for this op)
in : Check if the value of given key in data is exists in given val. val should be a plain Array.
notin : Check if the value of given key in data is not exists in given val. val should be a plain Array.
startswith : Check if the value of given key in data starts with (has a prefix of) the given val. This would only works for String type data.
endswith : Check if the value of given key in data ends with (has a suffix of) the given val. This would only works for String type data.
contains : Check if the value of given key in data has a substring of given val. This would only works for String type data.
match : Check if the value of given key in data has a Regular Expression match with the given val. The val parameter should be a RegExp for this op.
macro : It would try to match the value of given key in data executing the given val. The val parameter should be a Function for this op. This function should have a matching logic inside it and return true or false based on that.
example:
Let's say you want to find the 'users' who has id of 1. You can do it like this:
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.
For example, if you want to find the users with id of 1 or 2, you can do it like this:
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.