0

This one may be a tad tricky to understand if I'm not clear. I want to open a .json file, which contains an array and add/append to that array but I keep coming into format problems with the array. I first thought this needed to be PHP but I don't see why it couldn't be JS now.

Some may ask, why aren't I using a database, it just won't work for what I want to do. I just what to store data in a file on-site without a database.

analytics.php on execution adds the data to the data.json file. The plan is to call this on every visit (small site).

$visitArray = [];

$visitArray["ip"] = "127.0.0.1";
$visitArray["referrer"] = "www.google.com";
$visitArray["conversion"] = "nil";
$visitArray["bounce"] = "nil";
$visitArray["platform"] = "Chrome";
$VisiterArray[] = $visitArray;



$data = json_encode($VisiterArray);
$data = $data;
file_put_contents("data.json", $data, FILE_APPEND);

data.json (when analytics.php is executed)

[
   {
    "ip":"127.0.0.1",
    "referrer":"www.google.com",
    "conversion":"nil",
    "bounce":"nil",
    "platform":"Chrome"
   }
]

The problem is if you run the analytics.php there's no "," and I don't think it's as easy to solve to change $data = $data; to $data = $data.','; it just seems like a terrible way to do things.

    [
   {
    "ip":"127.0.0.1",
    "referrer":"www.google.com",
    "conversion":"nil",
    "bounce":"nil",
    "platform":"Chrome"
   }
]
    // missing ","
[
   {
    "ip":"127.0.0.1",
    "referrer":"www.google.com",
    "conversion":"nil",
    "bounce":"nil",
    "platform":"Chrome"
   }
]
4
  • 1
    Bad idea in so many ways Commented Sep 26, 2016 at 15:52
  • 1
    Thought it might be and the alt? Commented Sep 26, 2016 at 15:52
  • 1
    Why not just use a CSV with 5 values? Commented Sep 26, 2016 at 16:05
  • @apokryfos oh God... Commented Sep 26, 2016 at 16:11

2 Answers 2

2

What you want to do isn't easily achievable, as just adding a ',' would also result in invalid json (as your arrays would also need to be in an array)

Easiest solution would be just unserialise your JS and append it there. Something like

$VisiterArray = json_decode(file_get_contents("data.json"));
$visitArray = [];

$visitArray["ip"] = "127.0.0.1";
$visitArray["referrer"] = "www.google.com";
$visitArray["conversion"] = "nil";
$visitArray["bounce"] = "nil";
$visitArray["platform"] = "Chrome";
$VisiterArray[] = $visitArray;

$data = json_encode($VisiterArray);
file_put_contents("data.json", $data);

Although this isn't really ideal for logging.

If you really did want to do it that way, you could probably kinda cheat. Ie. append a "," after each entery like you suggested, then read it with something like $data = json_decode("[".file_get_contenst("data.json")."]"); which may work - although the file isn't really json at this point, so its a little hacky/nasty IMO. As others have suggested, a standard log file format probably makes more sense.

Sign up to request clarification or add additional context in comments.

9 Comments

The problem is this becomes slow very quickly. Still, the OP did say "small site."
Indeed, my suggestion is a pretty terrible idea for a logger :p Like you said, common log format makes a lot more sense
This crossed my mind but double handling it, is a tad too much. I think I'm starting to see the light with this approach, as in to move away from it completely. Is there a better way to store data to a similar effect without the downfalls and complexities ?
Assuming you have a standard set of columns, comma separated values are easy to work with + you can freely just append em to the end of your log.
and write to a txt file or something? thanks for your help I'll look into "common log format" as it means nothing to me, yet!
|
1

The problem isn't just a missing ,, even if you had a , there, it would be an invalid JSON file.

The problem you have is that both of the "container" types JSON has (array and object) require a termination character. Adding to the means inserting in front of that termination character — in your case, in front of that trailing ] so you can just add another object to the base array.

This makes JSON a less-than-ideal format for log files. Suggest using any of the standard HTTP log file formats, such as the common log format, and then converting to JSON as necessary after-the-fact.

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.