How to handle a large JSON file?

Trying build a storify feed on my site.

This works by pulling a story from the api (see http://dev.storify.com/api/stories) and parsing the returned json.

Now firstly im finding it difficult to get the correct elements from the json.

Here’s my code:


$story = get_file_contents('http://api.storify.com/v1/stories/storify/testimonials');
$data = json_decode($story);

echo $data->content...

Now I’m stuck after the bit about content. The problem is that the json is so huge and without line breaks I find it hard to see the tree structure and not sure what is a branch of which element.

Secondly, is this the best way of handling such a large json file? Couldn’t this potentially eat up huge amounts of memory?

You could also decode the JSON into an array if that’s easier for you. Then you can use print_r to get a better structural view of the data.


$data = json_decode($story, true);

echo "<pre>\
";
print_r($data);
echo "</pre>\
";

I’m not sure you’d have to worry too much about memory. It would have to be a very large feed. Just make sure you unset large variables when you’re done with them.


$data = json_decode($story, true);

// Do some stuff with the feed

unset($data);

Much more readable and accessible. Thanks! :slight_smile: