Solved

processing very big json file with php  - json_decode()

Posted on 2014-12-29
7
656 Views
Last Modified: 2015-01-06
I'm getting a memory exhausted error on json_decode(), how to resolve this ?
0
Comment
Question by:sniger
7 Comments
 
LVL 1

Accepted Solution

by:
Christian Vivas earned 500 total points
ID: 40522863
have you tried with memory_limit? upgrade the memory_max_limit in your php.ini to more than 128 lets say 512
0
 

Author Comment

by:sniger
ID: 40522869
other solutions ?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40522945
You've got the solution above - increase PHP's memory
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40522966
Please post a URL that gives us a link to the JSON string.   Then we can experiment with it and try to offer some assistance.  It may be as simple as increasing the memory limit, but it's probably going to require a surprisingly large limit.  PHP arrays and objects can run 20X the size of their string equivalents.  So if the JSON string is 4MB, you may be looking at 80MB just to hold the object of the decoded string.  Add to that any arrays or objects that are extracted in the course of processing the JSON object and you can see how this can domino into a huge memory requirement.

FWIW, JSON is a more economical transport mechanism than XML, and has mostly replaced XML in modern applications.

Once we can see the JSON string, if there is anything that can be done with memory limit, we can show you the requirement.  And if there is not any way to get an adequate memory limit, we may be able to suggest some workarounds.
0
 

Author Comment

by:sniger
ID: 40522968
what if 512 or 1024 is still too small ? How can I know the maximum size of a file which I'm going to get from external sources ?
0
 

Author Comment

by:sniger
ID: 40522970
Ray,

It is some simple key: value, array:
[
 {key1: val1, key2:val3, key3:val3,........},
  {key1: val1a, key2:val3a, key3:val3a,........},
  {key1: val1b, key2:val3b, key3:val3b,........}
.................................................,
]
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40523000
Hi, @sniger.  The key to this issue is the size of the JSON string, the number of nodes, etc.  That's why we need to see the test data set -- one that replicates the likely size of the real JSON strings.  We can't really answer the "what if" questions without seeing some test data.  It's rare that a JSON string is too large for processing with PHP, and since we're dealing with a rare thing here, there may not be a lot of preexisting solutions.
How can I know the maximum size of a file...
In a nutshell, you can't but with some test data we may be able to give you some useful strategies (I hope).
http://sscce.org/
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 29
html input clean up 3 32
php variable basic question 12 27
PHP json decode 9 20
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now