How to server-side process a big JSON file?
How to server-side process a big JSON file?
jsosa
Posts: 14Questions: 4Answers: 0
in DataTables
Hello, I currently have data sets of 2-3k rows, but may need to support around 25k rows. A full JSON file gets periodically generated with a background scheduled process, which is formatted appropriately for datatables.
How would I go about consuming that JSON file but with server-side processing in PHP? The examples I see on this site show using a database, but there's no database for this table. It's just the single JSON file.
Answers
'm not familiar with PHP but in general you using a flat file for this probably won't work well. Server side processing relies on protocol documented here and expects only the rows displayed on the page to be returned. Accessing the desired rows from a flat file would be cumbersome and probably not very performant. My recommendation would be to dump the data into a database for use with server side processing.
Kevin
I was able to figure out a way to do this with array_slice, along with ordering, custom filters, and searching on a couple specific columns that I allow (called id and key).
I think I'm ok for now, and I'll post what I did for any suggestions or for anyone else to reference.
Example Javascript:
Example PHP:
Nice, thanks for posting.
Colin
A few follow-up notes since I can't edit my post.
With more custom filters, I needed to add
"type": "POST"
to the ajax call since I got a URI too long error without it.The PHP function used for a simple string search,
strstr
, needs to bestristr
to be case insensitive. I also ended up replacing it with something likestripos($row['key'], $search) !== false
since the PHP docs say it's faster and less memory intensive. Regex could also be used. And a more generalized version of it should check each column's searchable value, probably as a helper function replacing the str functions it in my example.On client side, I wanted to note that I have custom filters as form fields where submit is tied to a listener that does a
table.draw();
after some validation.Thanks!