Playing with JSON in PHP

Recently, I asked for a list of web URLS to use for some software I’m testing, and I ended up receiving a zipped file containing a bunch of JSON files nested in a heap of subdirectories alongside some image files.

Well… I just wanted that one key-value pair from each JSON file within the subdirectories, which contained the URLs I desired.

I have access to a server with a PHP interpretor, so I decided to throw together a quick program to take care of the work of looping through the files and digging out the value I wanted from the JSON.


1) I used the RecursiveDirectoryIterator class to create an object (e.g. “di”) containing a bunch of  SplFileInfo objects, which represent everything found in that top level directory in which everything was originally unzipped (i.e. I created a PHP representation of the directory).

2) Next, I used the RecursiveIteratorIterator class and the “foreach” iterator to  and iterate through all the SplFileInfo objects within the “di” object. This allows me to look at all the files within the subdirectories.

3) Using Regular Expressions (in this case, the “preg_match” function), I was able to create a condition that would just be executed on files ending with the JSON file extension.

4) Using the “file_get_contents” function, I retrieved the contents from the JSON files and decoded them into PHP objects using the “json_decode” function.

5) Then, I pulled the “url” using the $object->value syntax.

At this point, you could do whatever you want with the urls. I decided to store them in an array and then print them out, but I could’ve just as easily printed them.

I’m certain that there are other ways to achieve the same effect, but it was nice being able to write a little program to look through a file system directory and extract the particular text that I wanted.

Now, I can use this as the basis for my next set of tests…