r/SQL • u/rospondek • Jan 22 '26
MySQL Uploading huge JSON file into mySQL database through PHP
OK guys this might be stupid problem but I'm already bouncing off the wall so hard the opposite wall is getting closer with every moment.
I need to upload very big JSON file into mySQL database to work with. File itself has around 430MB. The file itself is some public gov file pregenerated so I can't actually make it any more approachable or split into couple smaller files to make my life easier (as well as there is another problem mentioned a little later). If you need to see the mentioned file it is available here - https://www.podatki.gov.pl/narzedzia/white-list/ - so be my guest.
The idea is to make a website to validate some data with the data from this particular file. Don't ask why, I just need that, and it can't be done any different way. I also need to make it dumb friendly so anyone basically should just save the desired file to some directory and launch the webpage to update the database. I already did that with some other stuff and it if working pretty fine.
But here is the problem. File itself has over 3 mil rows and there is actually no database I have around, internal or external, which can get this file uploaded without error. I always get the error of
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 910003457 bytes) in json_upload.php
No matter the memory limit I set, the value needed is always bigger. So the question is. Is there any way to deal with such big JSON files? I read that they are designed to be huge but looks like not so much. I messed with the file a little and when I removed the data until around 415MB left it uploaded without any errors. I used to work with raw CSV files which are much easier to deal with.
Or maybe you have any hint what you do if you need to throw in such big data to database from a JSON file?
Thanks.
1
u/InlineSkateAdventure SQL Server 7.0 Jan 22 '26
Seems it is more a php upload issue? Such a huge file may require you to look into chunking/streaming data. That may be a bit tricky with a JSON conversion. I know node.js has solutions for this. Haven't used php in a while but there may be some large file addon.
2
u/Aggressive_Ad_5454 Jan 22 '26
The trouble with JSON is that each file contains a single document, and typical ways of reading the documents require slurping the whole thing then giving it to
json_decode()or whatever decoder. When the document is vast that blows out RAM, as you have discovered. If just reading the file doesn’t blow out the RAM, decoding it to create the php object still might.You need a so-called streaming or progressive decoder for this job. Here is one you might try. https://github.com/salsify/jsonstreamingparser
1
u/Pornolex Jan 22 '26
Create a local instance of mySQL and use the JSON import util with the locally saved file. Once you have the data in a mySQL friendly format use one of various dump-restore approaches to get it to the online DB (assuming you don't already have a shell on the online server)
1
u/FreeLogicGate Jan 23 '26
Decided to take on your challenge -- downloaded and unzipped the first file from the site you listed.
This was done on my m2 macbook pro.
I had a mysql 8.044 docker container version already set up for different project so I used that, which made this more complex that it would have been if I'd just used a mysql server. Obviously that is an old version of mysql, so there could be some small differences you encounter.
I created a test database and a table t.
I copied the json file into the docker container and placed it in the /tmp directory.
Initially I modified the max_allowed_packet setting using the mysql cli
When I ran the import the first time, I got an error stating I needed to increase the mysqlx_max_allowed_packet setting, so I also modified that to the same value
SET GLOBAL mysqlx_max_allowed_packet=524288000mysqlsh --defaults-file=/dev/null root@localhost/test --import /tmp/20260123.json t data
Cannot set LC_ALL to locale en_US.UTF-8: No such file or directory
WARNING: The --import option was deprecated and will be removed in a future version of the MySQL Shell. Please consider using the CLI call for import-json instead.
For additional information: mysqlsh -- util import-json --help
Please provide the password for 'root@localhost': ************
Save password for 'root@localhost'? [Y]es/[N]o/Ne[v]er (default No):
Importing from file "/tmp/20260123.json" to table `test`.`t` in MySQL Server at localhost:33060
.. 1.. 1
Processed 499.62 MB in 1 document in 27.7358 sec (0.04 document/s)
Total successfully imported documents 1 (0.04 document/s)
So there you have it -- took a total of 28 seconds to import the file.
A query?
As I don't understand the data in the file or what you are planning to do with it, I can't go much further, as just scanning it from the top, it appears to contain a large array of cryptic numbers, and I have no idea what you plan to do with this once you get it into a row in a database.
3
u/cl0ckt0wer Jan 22 '26
first you need to parse the json into a csv, then you can import the csv. there are plenty of tools for doing that
2
u/YahenP Jan 23 '26
Don't load JSON files into memory entirely. 400 MB can easily require more than 16 GB of memory. json_encode this is the wrong way. Use stream parsers.
forexampe, https://github.com/halaxa/json-machine
It's quite good, and I've used it several times in projects that required importing data from JSON . But there are other stream parsing libraries out there if you don't like this one.
1
u/[deleted] Jan 22 '26
Write a regex script to split each group of X JSON repeating sections into separate files that you dump in a folder and then loop through all of those JSON files in the folder and parse out the data and insert it. Removes any memory bottlenecks
1
u/FreeLogicGate Jan 22 '26 edited Jan 22 '26
To get this out of the way -- you need to provide some code if you want people to analyze what you are doing.
JSON with mysql is an add-on to the core RDBMS, and in this use case, may be of somewhat limited value at the point you actually succeed. MySQL doesn't just store it as a file, but actually converts the contents, which (last time I used it actively) has some issues with it, in that it will reorder elements in arrays. What this means is that in some cases if you load in a json file and then read it back out of the database, the order of some array elements will change, which can be an unpleasant surprise. I don't know if this was "fixed" or not since I encountered this issue, and it might not be of concern to you, but you should be aware of it.
I'm not sure what you are getting from MySQL of value, when it appears you have one json field per row with the entire contents. I'd suggest MongoDB but a single document (the equivalent of a mysql "row") has a maximum size of 16MB, so that tells you something about the advisability of not finding some way to break up this collection. Depending on the internal structure, there is no doubt in my mind you could find a way to break it up into logical pieces based on the internal structure.
As for PHP running out of memory, this is highly dependent on what your code is actually doing. You playing wack-a-mole with the memory limit setting doesn't mean there is no memory limit large enough -- there most certainly is, you just don't have any idea what that is, nor have you gained insight into how memory is being allocated currently, and why you are running out of it.
From your point of view, you seem to think that a 430mb file is the issue, and I can tell you that I have created PHP based code in the past that routinely downloaded, opened, parsed and inserted or updated substantially more data than that, and it did this daily and hourly, with the data sets coming from multiple sources.
The first thing you should do, is take php out of the equation. I also have to advise that for an activity like this, you should be creating a php cli application, but again we have no idea what code you wrote to do this import.
What you should be trying to do first is to use the mysqlsh cli utility to import the json file directly into your mysql db. The utility is documented here.
1
u/deWereldReiziger Jan 22 '26
You need to chunk the data. I had this issue using a csv with over 10,000 rows. Applying chunking fixed the issue
1
u/titpetric Jan 22 '26
https://github.com/titpetric/etl was built for such stuff, the CLI can pretty much do CRUD with json, also good for select->json. Go, so next best thing to php.
1
u/SrdelaPro Jan 23 '26
that error is a php interpreter memory limit and has nothing with do with mysql.
you have to change these in the interpreters .ini config memory_limit max_upload_filesize max_execution_time
there may be more limits that will have to be increased if you want to upload this via php in a single go
1
u/_steveCollins Jan 23 '26
Can you get the file on the MySQL server and import it through MySQL? Bypassing php entirely? I have a 5 mil row document and this is the only way it works for me.
1
u/recaffeinated Jan 23 '26
Open the file in VIM and manually split it into 8 smaller files.
I hope you're parsing the document into rows in sql and not just sticking the file into a single column.
1
u/kotysoft Jan 24 '26
Maybe you could give a try to an android app: GiantJSON viewer. After you opened the file in browser mode, and navigated into the array, you'll have EXPORT function. It can export SQL file, you can select fields, even rename them, and will generate you sql with insert statements, in batches. It's a streamer app, not trying to load it into memory
1
u/beavis07 Jan 25 '26
Rather than giving you more spurious, contextless technical suggestions, I’d like to advise you of the following:
Every one of the constraints you lay out here seem false. I would question all of them.
This feels to me very much like a small slice of a wider design issue.
We call this the A/B problem - instead of telling us the actual problem statement, you have just asked us about an issue with your solution.
Normally given questioning it becomes clear that the problem is then solution itself.
Without reference to any technologies at all, can you articulate the problem-statement?