Git Product home page Git Product logo

jsonpath's Introduction

JsonPath

Bitdeli Badge

JsonPath utility (XPath for JSON) for PHP based on Stefan Goessner's implementation : http://code.google.com/p/jsonpath/

C php extension is in progress here, any help is welcome :)

Documentation

What is JsonPath ? What is the syntax ? Take a look to Stefan Goessner's documentation

Installation

  • That's simple ! Add this to your composer.json :
    "require": {
        "peekmo/jsonpath": "dev-master"
    }

You just have to require the vendor/autoload.php (if not already) and add the following use :

    use Peekmo\JsonPath\JsonStore;

How it works ?

/!\ API Breaking Changes with new version !

Consider this json :

{ 
    "store": {
        "book": [ 
            { 
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            { 
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {  
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {   
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}
  • Transform your json into array (it works with object, but use an array for better performances)
  • You can get values like this :
    <?php
    
    require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload

    use Peekmo\JsonPath\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Returns an array with all categories from books which have an isbn attribute
    $res = $store->get("$..book[?(@.isbn)].category");
    
    $res = $store->get("$..book[?(@.isbn)].category", true); // You can set true to get only unique results

    ?>

It returns an array, you can so use default functions on the result (Have unique key for example) From 1.1.0, it returns an empty array if the node does not exists

  • You can change a value like this :
    <?php
    
    require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload

    use Peekmo\JsonPath\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Change the value of the first book's category
    $store->set("$..book[0].category", "superCategory");

    echo $store->toString();

    ?>

The value is passed by reference, so, when you are using a set, your object "$o" is modified. It returns a boolean to know if the node has been modified or not

  • You can add a value like this :
    <?php
    
    require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload

    use Peekmo\JsonPath\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Add a new value in first book's array "key":"value"
    $store->add("$..book[0]", "value", "key");

    echo $store->toString();

    ?>

The parameter "key" is optional, a number will be set if you're not providing one. It returns a boolean to know if the node has been modified or not

  • You can remove an attribute like this :
    <?php
    
    require_once __DIR__ . '/vendor/autoload.php'; // Autoload files using Composer autoload

    use Peekmo\JsonPath\JsonStore;

    $json = '...';

    $store = new JsonStore($json);

    // Removes the attribute "category" from all books
    $store->remove("$..book.*.category");

    echo $store->toString();

    ?>

Thanks

  • Special thanks to Stefan Goessner for his previous work

jsonpath's People

Contributors

adrienrn avatar gilo-agilo avatar martinec avatar peekmo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsonpath's Issues

Is it possible in array?

{"org":[{
"date": "20190330",
"organization": ["associated press"],
"persons": ["ahmed gaid salah", "abdelaziz bouteflika"],
"locations": ["algiers", "alger", "algeria"]
}]
}

I want to find, filter "algeria" locations.

Is it possible ?

Remote code execution security vulnerability

If an attacker can influence the json path expression, they can trivially run arbitrary code on your server.

Example expressions:

$[(phpinfo())]
$[?(phpinfo())]

The string phpinfo() can be replaced with any PHP code.

I hope nobody is still using this library.
We switched to https://github.com/Galbar/JsonPath-PHP which does not have this problem, as it does not use PHP's eval() function.

test case have a error

public function testGetAllByKeyFiltered()
{
$data = $this-&gt;jsonStore-&gt;get("$..book[(@.code=='02.01')].category");
$expected = ["fiction", "fiction"];
$this->assertEquals($data, $expected);
}

$data = $this-&gt;jsonStore-&gt;get("$..book[?(@.code=='02.01')].category"); ? discard

Callback for set in JsonStore

For JsonStore I need to use a callback for method set() to manipulate the existing value. I is very easy to implement, but for which version should I send a pull request?

The master looks not good for me to use it now in production. And for the tag 1.1 I can't find a equal branch.

Do you have a stable branch for version 1.x or can you create one?

There are also two versions to implement the callback:

  • We can check in the get method and call the callback.
  • We use a new method for example setByCallback.

Which version you prefer?

will only work with nested array conversion

Hi,

It seems in my php5 the sourcecode works only with array-ish jsonmaterial, and not with mixed nesting of arrays and objects.
After pulling my hair out, I found out that recursive pre-processing of the inputdata makes jsonpath selectors work.
You might want to include this in your JsonPath-class instead:

    $store = new Peekmo\JsonPath\JsonStore();
    $arr   = json_decode( $obj);        // will *not* work and break jsonpath
    $arr   = json_decode( $obj,true); // will *work*  this turns it into full associative array, no nested objects
    die( json_encode( $store->get( $arr, $input->path ) ) );

My guess is that jsonpath always returns arrays which breaks a selector when it hits objects.
So a json-string like:

{ "foo": { "bar":[1,2,3] } }

and a selector like

$..foo.bar

will not work because when the parser fetches 'foo', it gets an array-representation of "bar" (hence the '.bar'-part of the selector will break and return false).

Filters error on CLI

Hello

There's problems on the use of filters with PHP CLI (@see unit test testGetAllByKeyFiltered), the eval function seems to fail. If someone got an idea..

Regards,
Peekmo

Errors in UnitTest

You have different errors in your unit test:

  • In your composer.json you expects the php version >=5.3.3, but you use the new notation for arrays "[" and "]". You should write >=5.4 or change the unit test to use the old array() notation.
  • The assert values for expected and actual are switched
  • In Your example json you write "code": "01.02" on line 32, but on line 92 you check for code 02.01

BTW: in a library you shouldn't commit the composer.lock

Remove does not work

The remove functionality does not work. Have you tried the example that you put in Readme.md

New release

Hi, would you mind submitting a new release on Github? The latest commit is #f51947, and I'd like to reference that in my composer file as ~2.0 or something. Thanks!

Incompatible syntax

Instead of:

$.Result.Books[?(@.title="my book")]

I have to write:

$.Result.Books[?(@."title"=="my book")]

In order to prevent PHP notices, and to prevent assignment instead of comparisson. Because of eval.

PHP Notice:  Use of undefined constant title - assumed 'title' in /home/vagrant/application/src/vendor/peekmo/jsonpath/src/Peekmo/JsonPath/JsonPath.php(233) : eval()'d code on line 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.