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?

3
  • 2
    Already answered in php-json-decode-a-txt-file and stackoverflow.com/questions/6311813/… Commented Jun 11, 2011 at 0: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. Commented Jun 11, 2011 at 0:44
  • there are exactly 2 json functions in php - json_encode and json_decode. I suggest you to read the PHP manual Commented Jun 11, 2011 at 0:46

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

7 Comments

$json_data[$i] is actually an object, not an array. Your code won't work.
@Benjamin Morel, double check the documentation.
@Radu, run your code: Fatal error: Cannot use object of type stdClass as array. His JSON is an array of objects.
@Benjamin Morel, works just fine. You probably forgot to write the , true part on the json_decode() call.
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).
|
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.

Comments

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));

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.