PHP Classes
elePHPant
Icontem

PHP JSON Query: Query JSON data to find and extract information

Recommend this page to a friend!
  Info   View files Example   View files View files (16)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-07-20 (2 months ago) RSS 2.0 feedStarStarStarStar 74%Total: 520 This week: 2All time: 5,501 This week: 342Up
Version License PHP version Categories
jsonq 1.0.2The PHP License5PHP 5, Parsers
Description Author

This package can query JSON data to find and extract information.

It can parse JSON strings and perform queries to find nodes by name of object and array elements.

It can also filter by values of child element values using a fluent interface to combine multiple filter conditions.

Innovation Award
PHP Programming Innovation award winner
March 2017
Winner


Prize: One downloadable copy of Komodo IDE
JSON has become the de facto standard format for exchanging structured data types in the form of readable text, thus replacing XML for similar purposes.

There are many XML libraries for creating, parsing and querying XML documents.

This package provides a pure PHP solution for parsing and querying JSON data similar to XML query libraries.

Manuel Lemos
  Performance   Level  
Name: Nahid Bin Azhar <contact>
Classes: 9 packages by
Country: Bangladesh Bangladesh
Age: 26
All time rank: 136523 in Bangladesh Bangladesh
Week rank: 82 Up2 in Bangladesh Bangladesh Up
Innovation award
Innovation award
Nominee: 3x

Winner: 2x

Details

JsonQ (JSON Query)

JsonQ is a library for JSON data query for PHP and it also support laravel. You can import JSON file from anywhere in your project. Its handle and query your imported JSON data magically. So lets enjoy it.

Installation

JsonQ is a standalone package but it also support Laravel. To install this package run this command in your terminal from your desire project.

composer require nahid/jsonq

Instead, you may install it manually

{
    "require": {
        "nahid/jsonq": "^1.0"
    }
}

For laravel

Once JsonQ is installed, you need to register the service provider. Open up config/app.php and add the following to the providers key.

'Nahid\JsonQ\JsonqServiceProvider'

You can register the Jsonq facade in the aliases key of your config/app.php file if you like.

'Jsonq' => 'Nahid\JsonQ\Facades\Jsonq'

Configuration

There are no configuration if you use it without laravel. If you using laravel then you have to run this command to publish JsonQ config

php artisan vendor:publish --provider="Nahid\JsonQ\JsonqServiceProvider"

This will create config/jsonq.php file in your app that you can modify to set your desire configuration.

return [
	'json'=>[
		'storage_path'=>database_path()
	]
];

Here storage path means where you want to store all json data by default. Its not mandatory.

Usage

JsonQ has many convenient method to handle and query json data. So lets start how to use this package. Suppose you have a JSON file data.json and this is like the format

{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "info@example.com",
		"website":"www.example.com"
	},
	"items":[
		{"id":1, "name":"MacBook Pro 13 inch retina", "price": 1500},
		{"id":2, "name":"MacBook Pro 15 inch retina", "price": 1700},
		{"id":3, "name":"Sony VAIO", "price": 1200},
		{"id":4, "name":"Fujitsu", "price": 850}
	]
}

and you want to get description from this JSON data. So what you have to do? Don't be afraid, its so easy. First you have to import JSON data from file.

require 'vendor/autoload.php';

use Nahid\JsonQ\Jsonq;

$json=new Jsonq('path/to/data.json');

//or you may use
//$json->import('path/to/data.json');

$result = $json->node('description')->get();
echo $result;

For laravel

use Nahid\JsonQ\Facades\Jsonq;

Jsonq::import('path/to/data.json');

$result = Jsonq::node('description')->get();
echo $result;

The result is > Features product list

You also get nested node value by using colon ':' separator

$result = $json->node('vendor:email')->get();
echo $result;

Here is the result > info@example.com

Using Condition

You may also use condition but make sure this data is already formatted. JsonQ applied condition over formatted data.

Suppose we want an item where id is 2

$result = $json->node('items')->where('id', '=', 2)->get();

Here return a resultant array. But you you can get a single object for a single entity .

$result = $json->node('items')->where('id', '=', 2)->first();

Its return the first entity as an object.

Using Multiple Conditions

If we want a data where price greater than 1000 and less than 1600 then what should we do?

$result = $json->node('items')->where('price', '>', 1000)->where('price', '<', 1600)->get();

So here we'll get an array which price is greater than 1000 and less than 1600

Using orWhere

Don't worry here is orWhere.

$result = $json->node('items')->where('price', '==', 1200)->orWhere('id', '=', 3)->get();
  Files folder image Files  
File Role Description
Files folder imageconfig (1 file)
Files folder imageexample (1 file)
Files folder imagesrc (3 files, 1 directory)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file changelog.txt Doc. Documentation
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file data.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  config  
File Role Description
  Accessible without login Plain text file jsonq.php Conf. Configuration script

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageFacades (1 file)
  Plain text file JsonManager.php Class Class source
  Plain text file Jsonq.php Class Class source
  Plain text file JsonqServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file Jsonq.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageFacades (1 file)
  Plain text file JsonqServiceProviderTest.php Class Class source
  Plain text file TestCase.php Class Class source

  Files folder image Files  /  tests  /  Facades  
File Role Description
  Plain text file Jsonq.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:520
This week:2
All time:5,501
This week:342Up
 User Ratings  
 
 All time
Utility:95%StarStarStarStarStar
Consistency:90%StarStarStarStarStar
Documentation:85%StarStarStarStarStar
Examples:90%StarStarStarStarStar
Tests:-
Videos:-
Overall:74%StarStarStarStar
Rank:171