Download the PHP package skyscanner/jsonpath without Composer
On this page you can find all versions of the php package skyscanner/jsonpath. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download skyscanner/jsonpath
More information about skyscanner/jsonpath
Files in skyscanner/jsonpath
Package jsonpath
Short Description JSONPath implementation for querying and updating JSON objects
License Apache-2.0
Informations about the package jsonpath
JsonPath
This is a JSONPath implementation for PHP.
This implementation features all elements in the specification except the ()
operator (in the spcecification there is the $..a[(@.length-1)]
, but this can be achieved with $..a[-1]
and the latter is simpler).
In case of no matches for the query it will return an empty array.
Features
This implementation has the following features:
- Regex match key (p.e.
$.*[?(/^bo{2}k$/)]
or$[?(/a\wthors/)]
). - Regex match value comparisons (p.e.
$.store.book[?(@.author =~ /.*Tolkien/)]
) - For the child operator
[]
there is no need to surround child names with quotes (p.e.$[store][book, bicycle]
) except if the name of the field is a non-valid javascript variable name. .length
can be used to get the length of a string, get the length of an array and to check if a node has children.- The
in
operator allows filtering for a value in a specified list:$..[?(@.author in ["Nigel Rees", "Evelyn Waugh", $.store.book[3].author])]
- Object oriented implementation.
- Get, set and add operations.
- Magic methods implemented:
__get
:$obj->{'$...'}
.__set
:$obj->{'$...'} = $val
.__toString
:echo $obj
prints the json representation of the JsonObject.
- Not using
eval()
.
Installation
To install JsonPath you will need to be using Composer in your project. To install it please see the docs.
Usage
In every file you use it add:
Now you can create an instance of JsonObject and use it:
SmartGet
When creating a new instance of JsonObject, you can pass a second parameter to the constructor. This sets the behaviour of the instance to use SmartGet.
What SmartGet does is to determine if the given JsonPath branches at some point, if it does it behaves as usual;
otherwise, it will directly return the value pointed by the given path (not the
array containing it) or false
if not found.
GetJsonObjects
Sometimes you need to access multiple values of a subobject that has a long prefix (p.e. $.a.very.long.prefix.for.[*].object
), in this case you would first get said object
and make a JsonObject of it and then access its properties.
Now if you want to edit the object (set or add values) and want these changes to affect the original object, the way of doing this is by using
JsonObject::getJsonObjects($jsonpath)
. This method works the same way get does, but it will return the results as JsonObject instances containing a reference to the value in the source JsonObject.
JsonPath Language
This library implements the following specification:
¹var_name
: the regex roughly translates to "any valid JavaScript variable
name", plus some quirks such as names starting with numbers or containing
dashes (-
).
Limitations on the specification:
- The jsonpath inside value cannot contain
or
,and
or any comparator. - Jsonpaths in value return the first element of the set or
false
if no result. - Boolean operations can't be grouped with parethesis.
and
s are run beforeor
s. That means thata and 1 = b or c != d
is the same as(a and 1) or (c != d)
The .length
operator can be used to:
- Get the number of childs a node in the JsonObject has:
$..*[?(@.length > 3)]
- Filter for nodes that have childs:
$..*[?(@.length)]
- Or filter for nodes that don't have childs (leaves):
$..*[?(not @.length)]
- Check the length of a string:
$.path.to[?(@.a.string.length > 10)]
- Get the length of a string:
$.path.to.field.length
- Get the length of an array:
$.path.to.array.length
- Get the length of each array inside an array of arrays:
$.path.to.array[*].array[*].length
- Get the length of each string inside an array of strings:
$.path.to.array[*].array[*].key.length
The comparators:
==
, !=
, <
, >
, <=
, >=
do what expected (compare by type and value).
=~
is a regex comparator, matches the left operand with the pattern in the right operand. The value on the left must be a string and on
the right regpattern. Other wise returns false
.
JsonPath Example
Consider the following json:
JsonPath | Result |
---|---|
$.store.bicycle.price |
The price of the bicycle. |
$.store.book[*] |
All books. |
$.store.book[1,3] |
The second and fourth book. |
$.store.book[1:3] |
From the second book to the third. |
$.store.book[:3] |
From the first book to the third. |
$.store.book[x:y:z] |
Books from x to y with a step of z. |
$..book[?(@.category == 'fiction')] |
All books with category == 'fiction'. |
$..*[?(@.available == true)].price |
All prices of available products. |
$..book[?(@.price < 10)].title |
The title of all books with price lower than 10. |
$..book[?(@.author==$.authors[3])] |
All books by "J. R. R. Tolkien" |
$[store] |
The store. |
$['store'] |
The store. |
$..book[*][title, 'category', "author"] |
title, category and author of all books. |
$..book[?(@.author in [$.authors[0], $.authors[2]])] |
All books by "Nigel Rees" or "Herman Melville". |
$.store.book[?(@.category == 'fiction' and @.price < 10 or @.color == "red")].price |
Books of fiction with a price lower than 10 or something with of color red. (and takes precedence to or ) |
See more examples in the ./tests/Galbar/JsonPath
folder.
Test
To run tests, from the project root folder:
php app/test.php <jsonpath> [<file to json file>]
If no json file is given it defaults to the json object described previously in this file.
For example:
php app/test.php "$..*[?(@.category == 'fiction' and @.price < 10 or @.color == \"red\")].price"
Result should be:
[19.95,8.99]
Ready to code
you can open the project in your browser and can start coding
Docs
To generate the docs, from the project root folder:
php vendor/bin/sami.php update app/docgen.php