1

How do I add to a .json file with PHP? Currently, I'm appending a .json file with PHP, but it won't add the data to an existing JSON object. It makes a new object. I need the data all stored in one object, in an external JSON file. Basically, I have a JSON object and want to add more values to it.

$jsonFile = "test.json";
$fh = fopen($jsonFile, 'w');

$json = json_encode(array("message" => $name, "latitude" => $lat, "longitude" => $lon, "it" => $it));

fwrite($fh, $json);
5
  • 6
    I would json_decode it, array_merge it with the new array, and then json_encode it again. Commented Mar 1, 2013 at 2:18
  • Your saying to retrieve whats already there, then merge and re-encode the arrays? Then, re write it to the json file? Commented Mar 1, 2013 at 2:23
  • Precisely. I am not aware of any feasible in-place transformation that you might use. Commented Mar 1, 2013 at 2:27
  • As long as the data gets merged, I'm ok. Commented Mar 1, 2013 at 2:27
  • 2
    I've to say that mysql functions are absolete, use PDO or mysqli functions instead. Commented Mar 1, 2013 at 2:32

3 Answers 3

14

You can decode the json file to a php array, then insert new data and save it again.

<?php
$file = file_get_contents('data.json');
$data = json_decode($file);
unset($file);//prevent memory leaks for large json.
//insert data here
$data[] = array('data'=>'some data');
//save the file
file_put_contents('data.json',json_encode($data));
unset($data);//release memory
Sign up to request clarification or add additional context in comments.

Comments

2

what's suggested above is the hard way. i am considering there should be an easier way, to literally append an array into the json file.

here is the algo:

$handle=fopen($jsonFile);
fseek($handle,-1,SEEK_END);
fwrite($handle,$arrayToAdd);
fclose($handle);

but i am not sure it's more cpu/memory efficient to do so than reading the whole json file into memory, adding the array and then getting it stored.

1 Comment

Yes it did waste much less memory & CPU to do so. However when the data structure changes you need to modify your code for that.
0

this might help future poeple even tho its a old tread i had issues getting a array put into a json array at a specific location using push so after some horsing around i came up with: using array_pad(array1,size,array2)

"{MetaData":[ { } ], "pgstyle":{ }, "fonts":{ }, "models":{ }, "Page":[ { "Type":"Text", }, { "Type":"3Dmodel", "id":"3", "name":"3D gallery", "gallerylist":[ { "model":"Basic Vert frame", "Type":"imageGallery", "name":"vertical", "gId":3, "id":303, "LightIntense":7500, "LightColor":"#ffffcb", "offset":{ "x":-70, "y":0, "z":0 }, "rotation":{ "x":90, "y":180, "z":0 }, "font":"Wittgenstein-Bold.ttf" }, { "model":"Basic Horz frame", "Type":"imageGallery", "name":"Dark Images", "gId":1, "id":301, "LightIntense":5000, "LightColor":"#ffffcb", "font":"Wittgenstein-Bold.ttf", "offset":{ "x":0, "y":0, "z":0 }, "rotation":{ "x":90, "y":180, "z":0 } }, { "model":"Basic Horz frame", "Type":"imageGallery", "name":"Dark Light", "LightIntense":5000, "LightColor":"#ffffcb", "gId":2, "id":302, "offset":{ "x":70, "y":0, "z":0 }, "rotation":{ "x":90, "y":180, "z":0 }, "font":"Wittgenstein-Bold.ttf" } ]} ] }

//the data i want to insert into my json file 
$newData = 
array("model"=>"$model","Type"=>"imgGallery","name"=>"$Title","gId" =>
(int)$gId,"id"=> (int)$id ,"LightIntense"=>(int)$LightIntensity,
"LightColor"=>"$LightColor","font"=>"$font","offset"=>array("x"=>(int) 
($ox),"y"=>(int)$oy,"z"=>(int)$oz),
"rotation"=>array("x"=>90,"y"=>180,"z"=>0 ));
// getting size
$size = sizeof($JData['Page'][$key]['gallerylist'])+1;
//appending data
$JData['Page'][$key]['gallerylist']=array_pad($JData['Page'][$key] 
['gallerylist'],
$size,$newData);
           
alertGreenRefr(var_dump(json_encode($JData,true)));
$Jfile = json_encode($JData,true);
file_put_contents("PageDatatest.json", $Jfile);

RESULT : "{MetaData":[ { } ], "pgstyle":{ }, "fonts":{ }, "models":{ }, "Page":[ { "Type":"Text", }, { "Type":"3Dmodel", "id":"3", "name":"3D gallery", "gallerylist":[ { "model":"Basic Vert frame", "Type":"imageGallery", "name":"vertical", "gId":3, "id":303, "LightIntense":7500, "LightColor":"#ffffcb", "offset":{ "x":-70, "y":0, "z":0 }, "rotation":{ "x":90, "y":180, "z":0 }, "font":"Wittgenstein-Bold.ttf" }, { "model":"Basic Horz frame", "Type":"imageGallery", "name":"Dark Images", "gId":1, "id":301, "LightIntense":5000, "LightColor":"#ffffcb", "font":"Wittgenstein-Bold.ttf", "offset":{ "x":0, "y":0, "z":0 }, "rotation":{ "x":90, "y":180, "z":0 } }, { "model":"Basic Horz frame", "Type":"imageGallery", "name":"Dark Light", "LightIntense":5000, "LightColor":"#ffffcb", "gId":2, "id":302, "offset":{ "x":70, "y":0, "z":0 }, "rotation":{ "x":90, "y":180, "z":0 }, "font":"Wittgenstein-Bold.ttf" }, { "model":"Basic Horz frame", "Type":"imgGallery", "name":"asdf ", "gId":4, "id":304, "LightIntense":5000, "LightColor":"#ffffcb", "font":"Wittgenstein-Bold.ttf", "offset":{ "x":140, "y":0, "z":0 }, "rotation":{ "x":90, "y":180, "z":0 } } ] } ] }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.