json-query-wrapper is a wrapper for the popular command-line JSON processor "jq".
- Easy to use interface
- PHP data type mapping
$ composer require estahn/json-query-wrapper
test.json
:
{
"Foo": {
"Bar": "33"
}
}
Example 1:
$jq = JsonQueryWrapper\JsonQueryFactory::createWith('test.json');
$jq->run('.Foo.Bar'); # string(33)
Example 2:
$jq = JsonQueryWrapper\JsonQueryFactory::createWith('test.json');
$jq->run('.Foo.Bar == "33"'); # Returns bool(true)
Example 3:
$jq = JsonQueryWrapper\JsonQueryFactory::createWith('{"Foo":{"Bar":"33"}}');
$jq->run('.Foo.Bar == "33"'); # Returns bool(true)
Example 1:
$jq = JsonQueryWrapper\JsonQueryFactory::create();
$jq->setDataProvider(new JsonQueryWrapper\DataProvider\File('test.json');
$jq->run('.Foo.Bar == "33"'); # Returns bool(true)
A "Data Provider" provides the wrapper with the necessary data to read from. It's a common interface for several providers. All providers implement the DataProviderInterface
which essentially returns a path to the file for jq
.
Available providers:
Text
- Regular PHP string containing JSON dataFile
- A path to a file containing JSON data
- jmespath.php - Declaratively specify how to extract elements from a JSON document, in PHP
- JSONPath - JSONPath implementation for PHP (based on Stefan Goessner's JSONPath script)