umi85
umi85

Reputation: 11

Query Result to Json

I tried hard to find a way how to convert the result of an chyper query into a Json string or object, whatever.

I tried several ways.

$client = ClientBuilder::create()
->addConnection('default', 'http://XXX:[email protected]:7474')
->build();

$query = "MATCH p=()-[:BETEILIGTER]->()-[:UNTERPROJEKT]->() RETURN p LIMIT 100";
$result = $client->run($query);
$result->records();

foreach ($result->records() as $record) {
    print_r($record);      
    echo json_encode($record);

}

At the moment print_r($record) presents the data. However, json_encode returns only empty objects "{}". (1) I read about the problem that variables might be protected, which I found here but the sulution did not worked for me. (2) I tried to cast the variable:

foreach ($result->records() as $record) {
     $array =  (array) $record;
     echo json_encode($array);
}

But it didn't worked as well. The result was a bit more compared to the fist option:

{"\u0000*\u0000keys":["p"],"\u0000*\u0000values":[{}],"\u0000GraphAware\\Neo4j\\Client\\Formatter\\RecordView\u0000keyToIndexMap":{"p":0}}{"\u0000*\u0000keys":["p"],"\u0000*\u0000values":[{}],"\u0000GraphAware\\Neo4j\\Client\\Formatter\\RecordView\u0000keyToIndexMap":{"p":0}}

But still not a lot and not what I expected.

(3) I found a function in the source code of the neo4j-php-client called getJsonResponse(). But I can't call the procedure:

Call to undefined method GraphAware\Neo4j\Client\Formatter\Result::getJsonResponse() in /var/www/html/bpPhpHelper_4.php:15

and even don't know if this is a usefull approach.

Does anyone have a gues what to do? Thanks!

Here a small piece of the print_r($record) structure:

{"\u0000*\u0000keys":["p"],"\u0000*\u0000values":[{}],"\u0000GraphAware\\Neo4j\\Client\\Formatter\\RecordView\u0000keyToIndexMap":{"p":0}}GraphAware\Neo4j\Client\Formatter\RecordView Object
(
    [keys:protected] => Array
        (
            [0] => p
        )

    [values:protected] => Array
        (
            [0] => GraphAware\Neo4j\Client\Formatter\Type\Path Object
                (
                    [nodes:protected] => Array
                        (
                            [0] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 190256
                                    [labels:protected] => Array
                                        (
                                            [0] => tibikatPerson
                                        )

                                    [properties:protected] => Array
                                        (
                                            [idTibikatPerson] => 13767
                                            [sonstigeN] => 0
                                            [person] => XXX, XXX
                                            [verfasserN] => 0
                                            [herausgeberN] => 0
                                            [publicationN] => 1
                                            [mitwirkenderN] => 0
                                        )

                                )

                            [1] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 101334
                                    [labels:protected] => Array
                                        (
                                            [0] => fkatEinzelprojekt
                                        )

                                    [properties:protected] => Array
                                        (
                                            [zeOrt] => Dortmund
                                            [zeBland] => Nordrhein-Westfalen
                                            [ende] => 31.07.2015
                                            [stName] => Gesellschaft für Bildung und Beruf e.V.
                                            [stStadt] => Dortmund
                                            [fSum] => 272104
                                            [lpysyText] => Austausch mit anderen Staaten im Bereich der beruflichen Bildung sowie Stipendien
                                            [lpsys] => YB1000
                                            [tibikatUrl] => https://www.tib.eu/de/suchen/id/TIBKAT%3A869412949
                                            [fkz] => 01BEX01D12
                                            [zeName] => Gesellschaft für Bildung und Beruf e.V.
                                            [thema] => Verbundprojekt: Aus- und Weiterbildung für die Fachrichtung Tierwirt/in - Schweinehaltung, Kennwort: Tierwirt/in (China), Teilvorhaben: 'Interkulturelle Projektsteuerung und Öffentlichkeitsarbeit'
                                            [stOrt] => Dortmund
                                            [zeStadt] => Dortmund
                                            [beginn] => 01.08.2012
                                            [stBland] => Nordrhein-Westfalen
                                        )

                                )

                            [2] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 174977
                                    [labels:protected] => Array
                                        (
                                            [0] => fkatVerbundprojekt
                                        )

                                    [properties:protected] => Array
                                        (
                                            [einzelprojekteN] => 5
                                            [ressort] => BMBF
                                            [verbundprojektText] => Tierwirt China
                                            [pt] => PT-DLR
                                            [vkz] => 8732
                                            [fSumTotal] => 792895
                                            [ptEinheit] => BB
                                        )

                                )

                        )

                    [relationships:protected] => Array
                        (
                            [0] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
                                (
                                    [id:protected] => 115390
                                    [type:protected] => BETEILIGTER
                                    [startNodeIdentity:protected] => 190256
                                    [endNodeIdentity:protected] => 101334
                                    [properties:protected] => Array
                                        (
                                        )

                                )

                            [1] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
                                (
                                    [id:protected] => 76338
                                    [type:protected] => UNTERPROJEKT
                                    [startNodeIdentity:protected] => 101334
                                    [endNodeIdentity:protected] => 174977
                                    [properties:protected] => Array
                                        (
                                        )

                                )

                        )

                )

        )

    [keyToIndexMap:GraphAware\Neo4j\Client\Formatter\RecordView:private] => Array
        (
            [p] => 0
        )

)

Answer to OldPadawan suggestion:

Code was:

<?php


    require_once 'vendor/autoload.php';

    use GraphAware\Neo4j\Client\ClientBuilder;


    $client = ClientBuilder::create()
    ->addConnection('default', 'http://XXX:[email protected]:7474')
    ->build();

    $query = "MATCH p=()-[:BETEILIGTER]->()-[:UNTERPROJEKT]->() RETURN p LIMIT 100";

    $result = $client->run($query);

    $list = array();
    $element = array();


    foreach ($result->records() as $record) {
         // print_r($record);
         $element = "$record";
         array_push($list,$element);
         // $array =  (array) $record;
         // echo json_encode($array);
    }

        //echo json_encode($list);

?>

Error: PHP Catchable fatal error: Object of class GraphAware\Neo4j\Client\Formatter\RecordView could not be converted to string in /var/www/html/bpPhpHelper_6.php on line 23

Second Answer to OldPadawan suggestion:

You gave me this code

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

/* whatever params that are needed to be added here */

$client = ClientBuilder::create()
->addConnection('default', 'http://XXX:[email protected]:7474')
->build();

$query = "MATCH p=()-[:BETEILIGTER]->()-[:UNTERPROJEKT]->() RETURN p LIMIT 100";
$result = $client->run($query);
$result->records();

foreach ($result->records() as $record) {
    print_r($record);
}

echo json_encode($record);

?>

I changed the limit to "2" due the size of response.

This is the response:

GraphAware\Neo4j\Client\Formatter\RecordView Object
(
    [keys:protected] => Array
        (
            [0] => p
        )

    [values:protected] => Array
        (
            [0] => GraphAware\Neo4j\Client\Formatter\Type\Path Object
                (
                    [nodes:protected] => Array
                        (
                            [0] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 196717
                                    [labels:protected] => Array
                                        (
                                            [0] => tibikatPerson
                                        )

                                    [properties:protected] => Array
                                        (
                                            [idTibikatPerson] => 20228
                                            [sonstigeN] => 0
                                            [person] => Wallussek, Ulrich
                                            [verfasserN] => 0
                                            [herausgeberN] => 0
                                            [publicationN] => 2
                                            [mitwirkenderN] => 0
                                        )

                                )

                            [1] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 100424
                                    [labels:protected] => Array
                                        (
                                            [0] => fkatEinzelprojekt
                                        )

                                    [properties:protected] => Array
                                        (
                                            [zeOrt] => Halblech
                                            [zeBland] => Bayern
                                            [ende] => 30.11.2012
                                            [stName] => ErduMo-S. GmbH & Co. KG - Nebenstelle
                                            [stStadt] => Möhnesee
                                            [fSum] => 147904
                                            [lpysyText] => Austausch mit anderen Staaten im Bereich der beruflichen Bildung sowie Stipendien
                                            [lpsys] => YB1000
                                            [tibikatUrl] => https://www.tib.eu/de/suchen/id/TIBKAT%3A799010480
                                            [fkz] => BEX017010
                                            [zeName] => ErduMo-S. GmbH & Co. KG
                                            [thema] => Verbundprojekt: Grubensicherheit in chinesischen Steinkohlebergbau, Teilvorhaben: Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich des Arbeits- und Gesundheitsschutzes
                                            [stOrt] => Möhnesee
                                            [zeStadt] => Halblech
                                            [beginn] => 01.10.2010
                                            [stBland] => Nordrhein-Westfalen
                                        )

                                )

                            [2] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 170018
                                    [labels:protected] => Array
                                        (
                                            [0] => fkatVerbundprojekt
                                        )

                                    [properties:protected] => Array
                                        (
                                            [einzelprojekteN] => 3
                                            [ressort] => BMBF
                                            [vkz] => 3773
                                            [pt] => PT-DLR
                                            [verbundprojektText] => Grubensicherheit im chinesischen Steinkohlebergbau - Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich der Arbeits- und Grubensicherheit
                                            [fSumTotal] => 200623
                                            [ptEinheit] => BB
                                        )

                                )

                        )

                    [relationships:protected] => Array
                        (
                            [0] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
                                (
                                    [id:protected] => 152288
                                    [type:protected] => BETEILIGTER
                                    [startNodeIdentity:protected] => 196717
                                    [endNodeIdentity:protected] => 100424
                                    [properties:protected] => Array
                                        (
                                        )

                                )

                            [1] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
                                (
                                    [id:protected] => 76187
                                    [type:protected] => UNTERPROJEKT
                                    [startNodeIdentity:protected] => 100424
                                    [endNodeIdentity:protected] => 170018
                                    [properties:protected] => Array
                                        (
                                        )

                                )

                        )

                )

        )

    [keyToIndexMap:GraphAware\Neo4j\Client\Formatter\RecordView:private] => Array
        (
            [p] => 0
        )

)
GraphAware\Neo4j\Client\Formatter\RecordView Object
(
    [keys:protected] => Array
        (
            [0] => p
        )

    [values:protected] => Array
        (
            [0] => GraphAware\Neo4j\Client\Formatter\Type\Path Object
                (
                    [nodes:protected] => Array
                        (
                            [0] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 178170
                                    [labels:protected] => Array
                                        (
                                            [0] => tibikatPerson
                                        )

                                    [properties:protected] => Array
                                        (
                                            [idTibikatPerson] => 1681
                                            [sonstigeN] => 0
                                            [person] => Blümke, D.
                                            [verfasserN] => 0
                                            [herausgeberN] => 0
                                            [publicationN] => 2
                                            [mitwirkenderN] => 0
                                        )

                                )

                            [1] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 100424
                                    [labels:protected] => Array
                                        (
                                            [0] => fkatEinzelprojekt
                                        )

                                    [properties:protected] => Array
                                        (
                                            [zeOrt] => Halblech
                                            [zeBland] => Bayern
                                            [ende] => 30.11.2012
                                            [stName] => ErduMo-S. GmbH & Co. KG - Nebenstelle
                                            [stStadt] => Möhnesee
                                            [fSum] => 147904
                                            [lpysyText] => Austausch mit anderen Staaten im Bereich der beruflichen Bildung sowie Stipendien
                                            [lpsys] => YB1000
                                            [tibikatUrl] => https://www.tib.eu/de/suchen/id/TIBKAT%3A799010480
                                            [fkz] => BEX017010
                                            [zeName] => ErduMo-S. GmbH & Co. KG
                                            [thema] => Verbundprojekt: Grubensicherheit in chinesischen Steinkohlebergbau, Teilvorhaben: Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich des Arbeits- und Gesundheitsschutzes
                                            [stOrt] => Möhnesee
                                            [zeStadt] => Halblech
                                            [beginn] => 01.10.2010
                                            [stBland] => Nordrhein-Westfalen
                                        )

                                )

                            [2] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
                                (
                                    [id:protected] => 170018
                                    [labels:protected] => Array
                                        (
                                            [0] => fkatVerbundprojekt
                                        )

                                    [properties:protected] => Array
                                        (
                                            [einzelprojekteN] => 3
                                            [ressort] => BMBF
                                            [vkz] => 3773
                                            [pt] => PT-DLR
                                            [verbundprojektText] => Grubensicherheit im chinesischen Steinkohlebergbau - Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich der Arbeits- und Grubensicherheit
                                            [fSumTotal] => 200623
                                            [ptEinheit] => BB
                                        )

                                )

                        )

                    [relationships:protected] => Array
                        (
                            [0] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
                                (
                                    [id:protected] => 152287
                                    [type:protected] => BETEILIGTER
                                    [startNodeIdentity:protected] => 178170
                                    [endNodeIdentity:protected] => 100424
                                    [properties:protected] => Array
                                        (
                                        )

                                )

                            [1] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
                                (
                                    [id:protected] => 76187
                                    [type:protected] => UNTERPROJEKT
                                    [startNodeIdentity:protected] => 100424
                                    [endNodeIdentity:protected] => 170018
                                    [properties:protected] => Array
                                        (
                                        )

                                )

                        )

                )

        )

    [keyToIndexMap:GraphAware\Neo4j\Client\Formatter\RecordView:private] => Array
        (
            [p] => 0
        )

)
{}

The last piece "{}" is the result of "echo json_encode($record);" while the rest is procuded by "print_r($record);" There was no reported error.

Upvotes: 1

Views: 557

Answers (0)

Related Questions