in reply to Create JSON file in specific format
Following is an example that does what you are talking about, but uses a query from a MySQL DB for a data source instead of a text file. You mention a database but do not specify what DB you are interested in. There is a web page involved which requests the data which is why the CGI module is included, but that is probably not all that interesting to you. The code works as is which is why I post it the way I do minus a few mods such as passwords being obfuscated. I do used the pretty print, but as previously pointed out by others, one can simply turn it off as shown in the documentation.
While reading a csv file line by line has no real faults, you can also use a module such as DBD::CSV to treat your csv file just like a regular DB with a good set of SQL statements available to use.
That may be more than you want, or not. Just thought I would toss it out there in case you are interested in or familiar with SQL. Hope some of this is useful to you....
#!/usr/bin/perl use Modern::Perl qw(2014); use DBD::mysql; use JSON; use CGI::Carp qw(fatalsToBrowser); my @output; my $cgi = CGI->new; my %params = $cgi->Vars; my $params = \%params; my $json = JSON::XS->new(); $|=1; my $q; if ( ! $params->{'start'}) { $q = qq(select * from exhibit where timestamp > subtime(now(), + "24:0:0.0")); $params->{'interval'} = qq(24:0:0.0); } else { $q = qq(select * from exhibit where timestamp >= '$params{'sta +rt'}' and timestamp <= '$params{'end'}'); } my $db = "breakins"; my $host = "localhost"; #"192.168.0.3"; my $user = "xxx"; my $password = "xxxxxxx"; my $dsn = "DBI:mysql:database=$db;host=$host;port=3306"; my $dbh = DBI->connect($dsn, $user, $password ); # the query to the db is determined above using times passed in by the + web page # in this case, I do a bit of modification to individual rows as they +are put in @output so data is in the specific json format required # also add fields to each row which I don't want to store in the datab +ase as they are redundant. my $sth = $dbh->prepare($q); $sth->execute; while ( my $row = $sth->fetchrow_hashref ){ $row->{'label'} = $row->{'timestamp'} . "-" . $row->{'IP +'}; $row->{'display'} = 'true'; $row->{'recurring'} = 'false'; $row->{'color'} = 'FFEEBB'; $row->{'timestamp'} = iso_time($row->{'timestamp'}); push @output, $row; } $dbh->disconnect; # Make sure that the resultant JSON will print out all nice and pretty $json->pretty(1); #or condensed if this is set to 0 # $prettyJSON will contain the text string which is the Oh-So-Pretty v +ersion of $jsonStructure my $prettyJson = $json->encode(\@output); print $cgi->header(-type=>'application/json', -rel=>'exhibit-data'); print qq({\n"items":); print $prettyJson; print qq(}\n); sub getdate { my $ts = shift; my $arg = shift; my ($date, $time) = split('T', $ts); if ($arg eq 'd') { return $date; } else { return $time; } } sub iso_time { my $t = shift; my @tp = split(' ', $t); my $ret = join('T', @tp); return $ret; } =pod startDate: Date in which your activity starts. If it is a recurring ev +ent, then all dates within the startDate/endDate range may display th +e event. If your event is not recurring, make the startDate and endDa +te the same. endDate: The ending date for your event. startTime: The time your event starts. endTime: The time your event ends. display: Whether the calendar will display your event. Enter either "t +rue" or "false". label: The label for your event. This will be displayed on the calenda +r within each box for each event. description: Your event description. recurring: If your event is recurring, list the days of the week it oc +curs on. Use "M", "T", "W", "R", "F", "S", and "Sn" for "Monday", "Tu +esday", "Wednesday", "Thursday", "Friday", "Saturday", and "Sunday", +respectively. color: The background color of your event. Please enter a hex, rgb, or + color name. =cut
...the majority is always wrong, and always the last to know about it...
A solution is nothing more than a clearly stated problem...
|
---|