0

Here's my problem. I have a JSON file like this:

[
{
    "projectName": "test",
    "clientName": "test2",
    "dateValid": "2014-04-18",
    "account": {
        "accountAmount": null,
        "accountDate": "2014-04-19",
        "accountType": null
    },
    "total": {
        "totalAmount": null,
        "totalDate": "2014-04-18",
        "totalType": null
    }
}]

And I want PHP to open this file and add another object, so my file will look like this :

[
    {
        "projectName": "test",
        "clientName": "test2",
        "dateValid": "2014-04-18",
        "account": {
            "accountAmount": null,
            "accountDate": "2014-04-19",
            "accountType": null
        },
        "total": {
            "totalAmount": null,
            "totalDate": "2014-04-18",
            "totalType": null
        }
    },
    {
        "projectName": "test",
        "clientName": "test2",
        "dateValid": "2014-04-18",
        "account": {
            "accountAmount": null,
            "accountDate": "2014-04-19",
            "accountType": null
        },
        "total": {
            "totalAmount": null,
            "totalDate": "2014-04-18",
            "totalType": null
        }
    }
]

It should be quite simple, but I can't achieve that. I tried multiple ways to do that :

$file = 'base.json';
if(file_exists ($file)){
    echo 'base.json found';
    $fileContent = file_get_contents($file);
    $oldData = json_decode($fileContent, true);
    echo var_export($oldData);
}
else {
    echo 'base.json not found';
    $oldData = [];
}


echo $data;
$data = json_encode($data);
$oldData = json_encode($oldData);
echo $data; // debug
file_put_contents('base.json', '['.$data.','.$oldData.']');

Yeah, I putted lots of echo to debug the data process... What do I am missing ?

4
  • possible duplicate of php jsonencode to a file , format issue Commented Apr 22, 2014 at 17:47
  • That topic / its answers isn't what I'm looking for. Commented Apr 23, 2014 at 15:29
  • It's essentially the same answer that I gave below, which you accepted. Commented Apr 23, 2014 at 16:20
  • Well, I can't understand this topic and apply the solution to my problem :) Commented Apr 24, 2014 at 12:26

5 Answers 5

5

You're treating this like string manipulation, which is the dead wrong way to go about it. You need to combine the two structures while they're objects, before you re-encode them to JSON.

These three lines...

$data = json_encode($data);
$oldData = json_encode($oldData);
file_put_contents('base.json', '['.$data.','.$oldData.']');

Should be rewritten as...

// Create a new array with the new data, and the first element from the old data
$newData = array($data, $oldData[0]);
$newData = json_encode($newData);
file_put_contents('base.json', $newData);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanation. There's still one problem : $oldData[0] pick the first object in the array, when I want to pick all objects (my example wasn't clear). How can I pick all objects in $oldData ?
2

Add the new data to the array with:

$oldData[] = $data;

Then write it back to the file:

file_put_contents('base.json', json_encode($oldData));

2 Comments

Thanks this worked a lot, I didn't know this syntax : $array[] = $newStuff;
It's equivalent to array_push($oldData, $data)
2

You can transform your json var into array specifing the second param to true on json_decode and after use array_merge to include the new array var and convert to json again.

<?php
$json1 = '[{
    "projectName": "test",
    "clientName": "test2",
    "dateValid": "2014-04-18",
    "account": {
        "accountAmount": null,
        "accountDate": "2014-04-19",
        "accountType": null
    },
    "total": {
        "totalAmount": null,
        "totalDate": "2014-04-18",
        "totalType": null
    }
}]';

$json2 = '[{
    "projectName": "test 2",
    "clientName": "test3",
    "dateValid": "2014-04-22",
    "account": {
        "accountAmount": null,
        "accountDate": "2014-04-27",
        "accountType": null
    },
    "total": {
        "totalAmount": null,
        "totalDate": "2014-04-27",
        "totalType": null
    }
}]';

$arr1 = json_decode($json1, true);
$arr2 = json_decode($json2, true);

$json2 = json_encode(array_merge($arr1, $arr2));

?>

Comments

1

try this

$arr = json_decode(file_get_contents('myFile.json'));

// append a new "object" (array)
$arr[] = array(
    "projectName" => "test",
    "clientName" => "test2",
    "dateValid" => "2014-04-18",
    "account" => array(
        "accountAmount" => null,
        "accountDate" => "2014-04-19",
        "accountType" => null
    ),
    "total" => array(
        "totalAmount" => null,
        "totalDate" => "2014-04-18",
        "totalType" => null
    )
);

$json = json_encode($arr);

file_put_contents('myFile.json', $json);

Comments

0

try this:

<?php


$json = '[
{
    "projectName": "test",
    "clientName": "test2",
    "dateValid": "2014-04-18",
    "account": {
        "accountAmount": null,
        "accountDate": "2014-04-19",
        "accountType": null
    },
    "total": {
        "totalAmount": null,
        "totalDate": "2014-04-18",
        "totalType": null
    }
}]';

$json_to_add=' {
        "projectName": "test",
        "clientName": "test2",
        "dateValid": "2014-04-18",
        "account": {
            "accountAmount": null,
            "accountDate": "2014-04-19",
            "accountType": null
        },
        "total": {
            "totalAmount": null,
            "totalDate": "2014-04-18",
            "totalType": null
        }
    }';

$data = json_decode($json);
$data_to_add = json_decode($json_to_add);
$data[]=$data_to_add ;
var_dump($data);

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.