nahid / jsonq

A PHP query builder for JSON

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

array_key_exists() expects parameter 2 to be array, string given

RoNoLo opened this issue · comments

Hello,

I seem to have trouble using this library correctly. My goal is to use it as a JSON query tool on a directory of equal or different structured JSON files. Therefore I most likely do not have a huge json with a collection of data available.

Further I just want to get back a TRUE or the wohle object when my conditions are match. With my trys I get always the error message from the issue title.

Simple example from the readme.md:

$q = new Jsonq();
$q->collect(['id'=>1, 'name'=>'Nahid']);
$res = $q->where('name', '=', "Nahid")->get();

or

$q = new Jsonq('data.json'); // data from the README.md
$res = $q->where('name', '=', "products")->get();

When I try something else it returns empty even though I can see that it should match.

$q = new Jsonq();
$q->collect(['foo' => ['id'=>1, 'name'=>'Nahid']]);
$res = $q->where('foo.name', '=', "Nahid")->get();
// $res is empty array

Full Error:
array_key_exists() expects parameter 2 to be array, string given
...\nahid\jsonq\src\JsonQueriable.php:334
...\nahid\jsonq\src\JsonQueriable.php:386
...\nahid\jsonq\src\JsonQueriable.php:393
...\nahid\jsonq\src\JsonQueriable.php:125
...\nahid\jsonq\src\Jsonq.php:120

Im having the same problem.

Any ideas on this one?

class C\A\MicroService
public function handle(string $json) : array
    {
        $jsonq = new Jsonq();
        $jsonq->json($json);

        $matchLevel = $jsonq->from('matchLevel')->get();

        if($matchLevel != 'Street number'){
            throw new \Exception('To low matchLevel, level is ' . $matchLevel );
        }

        $res = $jsonq->from('products')->get();
        return $res;
    }
PHP Warning:  array_key_exists() expects parameter 2 to be array, string given in /var/app/microservices/vendor/nahid/jsonq/src/JsonQueriable.php on line 334
PHP Stack trace:
PHP   1. {main}() /var/app/microservices/test/index.php:0
PHP   2. C\A\MicroService->handle() /var/app/microservices/test/index.php:23
PHP   3. Nahid\JsonQ\Jsonq->get() /var/app/microservices/src/single.php:48
PHP   4. Nahid\JsonQ\Jsonq->prepare() /var/app/microservices/vendor/nahid/jsonq/src/Jsonq.php:120
PHP   5. Nahid\JsonQ\Jsonq->getData() /var/app/microservices/vendor/nahid/jsonq/src/JsonQueriable.php:135
PHP   6. Nahid\JsonQ\Jsonq->getFromNested() /var/app/microservices/vendor/nahid/jsonq/src/JsonQueriable.php:359
PHP   7. array_key_exists() /var/app/microservices/vendor/nahid/jsonq/src/JsonQueriable.php:334

EDIT:
Made a workaround that "works".

// class C\A\MicroService
public function handle(string $json) : array
{
        $jsonq = new Jsonq();
        $jsonq->json($json);

        // Repeated the object, clone does not work
        $jsonq2 = new Jsonq();
        // Run json method again. 
        $jsonq2->json($json);

        // Use the second json here
        $matchLevel = $jsonq2->from('matchLevel')->get();

        if($matchLevel != 'Street number'){
            throw new \Exception('To low matchLevel, level is ' . $matchLevel );
        }

        //Use the first json here
        $res = $jsonq->from('products')->get();
        return $res;
}