4

Here is a JSON data example saved in a file data.txt

   [
     {"name":"yekky"},
     {"name":"mussie"},
     {"name":"jessecasicas"}
     ...// many rows
    ]

I would like to update the file so it will look like this:

[
 {"name":"yekky","num":"1"},
 {"name":"mussie","num":"2"},
 {"name":"jessecasicas","num":"3"}
 ...// many rows
]

This is what I have got so far:

$json_data = file_get_contents('data.txt');
// What goes here?

And how do I count how many rows there are in the JSON tree?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
fish man
  • 2,666
  • 21
  • 54
  • 94
  • 2
    Already answered in [php-json-decode-a-txt-file](http://stackoverflow.com/questions/6312726/php-json-decode-a-txt-file) and http://stackoverflow.com/questions/6311813/php-how-to-write-data-into-json – J.C. Inacio Jun 11 '11 at 00:38
  • @jcinacio, I am studing json now. So first question is how to write, now is how to update. it is easy for you, but not easy for a student. – fish man Jun 11 '11 at 00:44
  • there are exactly **2** json functions in php - json_encode and json_decode. I suggest you to read the PHP [manual](http://www.php.net/manual/en/) – J.C. Inacio Jun 11 '11 at 00:46

3 Answers3

6

Use json_decode() to decode the JSON data in a PHP array, manipulate the PHP array, then re-encode the data with json_encode().

For example:

$json_data = json_decode(file_get_contents('data.txt'), true);
for ($i = 0, $len = count($json_data); $i < $len; ++$i) {
    $json_data[$i]['num'] = (string) ($i + 1);
}
file_put_contents('data.txt', json_encode($json_data));
rid
  • 61,078
  • 31
  • 152
  • 193
  • 1
    `$json_data[$i]` is actually an object, not an array. Your code won't work. – BenMorel Jun 11 '11 at 00:42
  • @Benjamin Morel, double check the documentation. – rid Jun 11 '11 at 00:43
  • @Radu, run your code: `Fatal error: Cannot use object of type stdClass as array`. His JSON is an array of objects. – BenMorel Jun 11 '11 at 00:46
  • @Benjamin Morel, works just fine. You probably forgot to write the `, true` part on the `json_decode()` call. – rid Jun 11 '11 at 00:48
  • You're right, it does indeed with `$assoc = true`. Still found it misleading though to convert to a PHP array, then let `json_encode()` convert that back to a JS object (because the keys are not numeric). – BenMorel Jun 11 '11 at 00:53
  • `json_encode()` converts it to an array. The keys are indeed numeric, because the source is an array. – rid Jun 11 '11 at 00:54
  • I mean the `{"name":"yekky","num":"1"}` part, which is a JS object. It was a PHP associative array, which as no equivalent in JS, so it is converted to an object by `json_encode()`. I gave you a +1 for mentioning my copy/paste mistake below :) – BenMorel Jun 11 '11 at 00:58
2

You should use the PHP JSON library for such tasks. For example, after having read your JSON data from the file, do something like:

$json = json_decode($json_data);
$itemCount = count($json);

After having modified your JSON data, just encode it again:

$json_data = json_encode($json);

Also, you seem to want to beatify your JSON data. My advise is to just use whatever comes out of json_encode and save that to your file, because it will probably be the smallest (in file size) possible representation of your JSON data.

If you format it in a way readable for humans, you've got lots of extra spaces / tabs / line-breaks which increase file size and parsing time.

If you need to read it yourself, you can still beautify your JSON data by hand.

Community
  • 1
  • 1
fresskoma
  • 25,481
  • 10
  • 85
  • 128
2
$file = 'data.txt';
$data = json_decode(file_get_contents($file));
foreach ($data as $key => $obj) {
    $obj->num = (string)($key+1);
}
file_put_contents($file, json_encode($data));
BenMorel
  • 34,448
  • 50
  • 182
  • 322