Peekmo/JsonPath

will only work with nested array conversion

Closed this issue · 2 comments

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).

Hello,

Sorry for the time to answer, I can't produce the problem.
Could you please, test with the new alpha version, it should resolve your problem in my opinion, everything is now use as array.

Regards,
Peekmo

Thnx. Works now.
I cloned the last version, and ran the following test.
Both array / object input works now, see this output:

testing '$..foo.bar' on { "foo": { "bar":[1,2,3] } }:
output: [[1,2,3]]

testing '$..foo.bar' on { "foo": { "bar":[1,2,3] } }:
output: [[1,2,3]]

Using this test

<?
include_once("src/Peekmo/JsonPath/JsonStore.php");
include_once("src/Peekmo/JsonPath/JsonPath.php");
$json  = '{ "foo": { "bar":[1,2,3] } }'; 
$path  = "$..foo.bar";
$obj   = json_decode( $json);        // will *not* work and break jsonpath    
$arr   = json_decode( $json,true); // will *work*  this turns it into full associative array, no nested objects

$store = new Peekmo\JsonPath\JsonStore($arr);
echo "testing '{$path}' on {$json}:\n";  
echo "output: ".json_encode( $store->get( $path ) )."\n\n";

$store = new Peekmo\JsonPath\JsonStore($obj);
echo "testing '{$path}' on {$json}:\n";  
echo "output: ".json_encode( $store->get( $path ) )."\n\n";
?>