0

I want to get a specific part of the file instead of using the file_get_contents() function to pull the whole file. For example i have a json file like below.

{
   "0": {
     "id":1,
     "name":"test1",
   },
   "1": {
     "id":2,
     "name":"test2",
   }
}

As above, there is a json data with 10,000 keys. And there are more than 1500 files like this. Among these files, I want to get the part with a data with id = 4999, for example, from a file with the above data.

Is the only way around this by pulling the entire contents of the file and decoding it and then searching? Or is there an easier way?

The reason I'm asking this is, for example, there will be 10 different data on a page. And this data may be the information contained in the content of 10 different files. When I pull and decode all 10 of these files, an array will have 100,000 pieces of data. Will this slow down the system?

emrez
  • 316
  • 2
  • 11
  • 1
    _"Will this slow down the system?"_ - if you're worried about that, then you should not be reading data from multiple (large) files to begin with - but put that data into a database. – CBroe Jun 17 '22 at 11:29
  • json is encoded, so you will have to pull everything out of the file to decode it. It's not like a CSV where you can just parse it line by line. – aynber Jun 17 '22 at 11:30

1 Answers1

1

Is the only way around this by pulling the entire contents of the file and decoding it and then searching?

....yes. Because if you pull only part of the file then the retrieved data won't form a valid JSON object and thus won't be decodable, and therefore you won't be easily able to access any of the data.

P.S. I guess you could try to use some text searching / parsing in that scenario, e.g. with regular expressions to try and find the individual object, but it's likely to be difficult/impossible to achieve, and potentially quite brittle. I wouldn't recommend it at all.

P.P.S. If you have this quantity of data and regularly need to retrieve specific information from it, I'd suggest that maybe a database is a better place to store it than a series of large JSON files.

ADyson
  • 57,178
  • 14
  • 51
  • 63