SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: How to handle a large JSON file?

  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP 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?

  2. #2
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    $data json_decode($storytrue);

    echo 
    "<pre>\n";
    print_r($data);
    echo 
    "</pre>\n"
    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.
    PHP Code:
    $data json_decode($storytrue);

    // Do some stuff with the feed

    unset($data); 

  3. #3
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Much more readable and accessible. Thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •