convert neo4j database to json file using PHP - php

I want to know how can I do to convert neo4j database to json file using PHP.
I used the code below but i want to know the similar of mysqli_fetch_assoc in Neo4j:
$req= "match n return n";
$result = $client->sendCypherQuery($req)->getResult(); //create an array
$emparray[] = array();
while($row =mysqli_fetch_assoc($result)) { $emparray[] = $row; }
echo json_encode($emparray); //write to json file
$fp = fopen('data/charlize.json', 'w');
fwrite($fp, json_encode($emparray));
fclose($fp);

to get all of your database as a JSON you can simply send a post request to http://<your database ip>/db/data/cypher with param "query" : "Match a return a" this will return a JSON string as response, then you can do whatever you want (print it in a file for example).

Related

Converting json to csv returns array,array

I'm trying to convert a json file to csv but only the text "array,array" gets printed in the csv file. I'm guessing this is because multiple arrays get returned but as a noob i dont know how to fix this.
<?php
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$jsonDecoded = $jsonDecoded;
//Give our CSV file a name.
$csvFileName = 'example.csv';
//Open file pointer.
$fp = fopen($csvFileName, 'w');
//Loop through the associative array.
foreach($jsonDecoded as $row){
//Write the row to the CSV file.
fputcsv($fp, $row);
}
//Finally, close the file pointer.
fclose($fp);
print $jsonDecoded;
echo json_last_error_msg();
?>
The json looks like this, contains multiple records and should be printed each on a row.
{"data":[{"ID":4,"UUID":"53F","A schematic overview of your activities":"Yes","Q1_1-1":"To some extent","Q1_1-2":"To some extent","Q1_1-3":"To some extent","Question 1_2":"Yes","Q1_2-1":"Yes","Q1_2-2":"To some extent","Q1_2-3":"No","Q1_2-4":"Yes","Q1_2-5":"To some extent","Q1_2-6":"Yes","Question 1_3":"Yes","Q1_3-1":"Yes","Q1_3-2":"To some extent","Q1_3-3":"To some extent","Q1_3-4":"No","Q1_3_5":"Yes","Question 2":"To some extent","Q2_2":"To some extent","Q2_3":"To some extent","Q2_4":"To some extent","Question 3":"No","Q3_2":"No","Q3_3":"To some extent","Q3_4":"Yes","Question 3_2":"Yes","Q3_2-2":"Yes","Q3_2-3":"To some extent","Question 3_3":"No","Q3_3-2":"To some extent","Q3_3-3":"Yes","Q3_3-4":"To some extent","Question 3_4":"Yes","Q3_4-2":"To some extent","Q3_4-3":"To some extent","Q3_4-4":"To some extent","Question 3_5":"Yes","Q3_5-2":"To some extent","Q3_5-3":"Yes","Q3_5-4":"Yes","Q3_5_5":"To some extent","Q3_5-6":"To some extent","Question 3_6":"Yes","Q3_6-2":"Yes","CreatedAt":"2019-08-14T10:38:07.033Z","CreatedBy":"qqq","UpdatedAt":null,"UpdatedBy":null,"CreatedByID":20,"UpdatedByID":null},{"ID":5,"UUID":"2D40","A schematic overview of your activities":"Yes","Q1_1-1":"To some extent","Q1_1-2":"To some extent","Q1_1-3":"Yes","Question 1_2":"Yes","Q1_2-1":"To some extent","Q1_2-2":"No","Q1_2-3":"To some extent","Q1_2-4":"Yes","Q1_2-5":"Yes","Q1_2-6":"To some extent","Question 1_3":null,"Q1_3-1":null,"Q1_3-2":null,"Q1_3-3":null,"Q1_3-4":null,"Q1_3_5":null,"Question 2":null,"Q2_2":null,"Q2_3":null,"Q2_4":null,"Question 3":"No","Q3_2":"To some extent","Q3_3":"To some extent","Q3_4":"To some extent","Question 3_2":"Yes","Q3_2-2":"To some extent","Q3_2-3":"Yes","Question 3_3":"Yes","Q3_3-2":"No","Q3_3-3":"To some extent","Q3_3-4":"Yes","Question 3_4":"Yes","Q3_4-2":"To some extent","Q3_4-3":"Yes","Q3_4-4":"Yes","Question 3_5":"No","Q3_5-2":"To some extent","Q3_5-3":"To some extent","Q3_5-4":"Yes","Q3_5_5":"To some extent","Q3_5-6":"To some extent","Question 3_6":"Yes","Q3_6-2":"To some extent","CreatedAt":"2019-08-19T13:48:22.770Z","CreatedBy":"qqq","UpdatedAt":null,"UpdatedBy":null,"CreatedByID":20,"UpdatedByID":null}]}
You just need to refer to the 'data' key.
foreach($jsonDecoded['data'] as $row){ ...
https://www.php.net/manual/en/splfileobject.fputcsv.php
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$list= $jsonDecoded;
file = new SplFileObject('example.csv', 'w');
foreach ($list as $fields) {
$file->fputcsv($fields);
}

PHP input to JSON file and JSON file to PHP output

Can anyone please shows me how to get the input of PHP to JSON file (.json) and read data from JSON file and display in PHP (Echo).
for example:
$myObj->name = "John";
$myObj->age= 20;
to result.json
{"name":"John","Age":20}
and retrieve from result.json and display data in PHP as
name=John
Age=20
To convert the object to json use this:
$json = json_encode($myObj);
See the json_encode docs.
To return it back to the format you want try this...
$obj = json_decode($json);
$name = $obj->name; // John
$age = $obj->age; // 20
See the json_decode docs.
To iterate over keys and values do something like this:
foreach($obj as $key=>$value)
{
echo $key . " = " . $value . "\n";
}
json_encode() is used for encoding PHP data into a JSON format and json_decode() is used to decode JSON into a PHP data
json_encode documentation:
http://php.net/manual/en/function.json-encode.php
json_decode documentation
http://php.net/manual/en/function.json-decode.php
if you are having an php array you can convert it into json.
$json_string = json_encode($array);
and write this into a json file.
$fp = fopen('results.json', 'w');
fwrite($fp, json_string);
fclose($fp);
now convert your json string which is in results.json to array.
$str = file_get_contents('./results.json');
$array = json_decode($str, true); // decode the JSON into an associative array

posting data from sqlite to mysqldb using json

I am trying to get JSON posted by an android application and decode it into an array to store it in my sqldb.
I have written the following php code. When I execute it, it displays [] (line 6).
<?php
include_once './db_functions.php';
//Create Object for DB_Functions class
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = (isset($_POST["usersJSON"])? $_POST["usersJSON"] : '');
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->storeUser($data[$i]->userId,$data[$i]->userName);
//Based on insertion, create JSON response
if($res){
$b["id"] = $data[$i]->userId;
$b["status"] = 'yes';
array_push($a,$b);
}else{
$b["id"] = $data[$i]->userId;
$b["status"] = 'no';
array_push($a,$b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);
?>

Modify text file with php code

I have a JSON file badly formatted (doc1.json):
{"text":"xxx","user":{"id":96525997,"name":"ss"},"id":29005752194568192}
{"text":"yyy","user":{"id":32544632,"name":"cc"},"id":29005753951977472}
{...}{...}
And I have to change it in this:
{"u":[
{"text":"xxx","user":{"id":96525997,"name":"ss"},"id":29005752194568192},
{"text":"yyy","user":{"id":32544632,"name":"cc"},"id":29005753951977472},
{...},{...}
]}
Can I do this in a PHP file?
//Get the contents of file
$fileStr = file_get_contents(filelocation);
//Make proper json
$fileStr = str_replace('}{', '},{', $fileStr);
//Create new json
$fileStr = '{"u":[' . $fileStr . ']}';
//Insert the new string into the file
file_put_contents(filelocation, $fileStr);
I would build the data structure you want from the file:
$file_path = '/path/to/file';
$array_from_file = file($file_path);
// set up object container
$obj = new StdClass;
$obj->u = array();
// iterate through lines from file
// load data into object container
foreach($array_from_file as $json) {
$line_obj = json_decode($json);
if(is_null($line_obj)) {
throw new Exception('We have some bad JSON here.');
} else {
$obj->u[] = $line_obj;
}
}
// encode to JSON
$json = json_encode($obj);
// overwrite existing file
// use 'w' mode to truncate file and open for writing
$fh = fopen($file_path, 'w');
// write JSON to file
$bytes_written = fwrite($fh, $json);
fclose($fh);
This assumes each of the JSON object repsentations in your original file are on a separate line.
I prefer this approach over string manipulation, as you can then have built in checks where you are decoding JSON to see if the input is valid JSON format that can be de-serialized. If the script operates successfully, this guarantees that your output will be able to be de-serialized by the caller to the script.

PHP export JSON to CSV

Im try export json result to csv and save data as file, im try with something like this
$getFile = file_get_contents('JSON_URL');
$json_obj = json_decode($getFile);
$fp = fopen('/home/xxxx/public_html/xxxx/api/export/tmp/file.csv', 'w');
foreach ($json_obj as $row) {
fputcsv($fp, $row);
}
fclose($fp);
but seems not working
Here's an example json format for link above
[
{key:value,key:value...}
...]
in order for your code to work as expected, try decoding the json object as an associative array. This is done by passing a boolean true to the 2nd param of json_decode
$json_obj = json_decode($getFile, true);

Categories