2

I'm writing PHP code that uses a database. To do so, I use an array as a hash-map. Every time content is added or removed from my DB, I save it to file.

I'm forced by my DB structure to use this method and can't use mysql or any other standard DB (School project, so structure stays as is).

I built two functions:

function saveDB($db){
    $json_db = json_encode($db);
    file_put_contents("wordsDB.json", $json_db);
} // saveDB

function loadDB(){
    $json_db = file_get_contents("wordsDB.json");
    return json_decode($json_db, true);
} // loadDB

When echo-ing the string I get after the encoding or after loading from file, I get a valid json (Tested it on a json viewer) Whenever I try to decode the string using json_decode(), I get null (Tested it with var_dump()).

The json string itself is very long (~200,000 characters, and that's just for testing).

I tried the following:

  • Replacing single/double-quotes with double/single-quotes (Without any backslashes, with one backslash and three backslashes. And any combination I could think of with a different number of backslashes in the original and replaced string), both manually and using str_replace().
  • Adding quotes before and after the json string.
  • Changing the page's encoding.
  • Decoding without saving to file (Right after encoding).
  • Checked for slashes and backslashes. None to be found.
  • Tried addslashes().
  • Tried using various "Escape String" variants.

json_last_error() doesn't work. I get no error number (Get null, not 0).

It's not my server, so I'm not sure what PHP version is used, and I can't upgrade/downgrade/install anything.

I believe the size has something to do with it, because small strings seem to work fine.

Thanks Everybody :)

3
  • Zip your actual file, upload it somewhere for anyone else to test. Else this might become a guessing game. || Various failure causes include a UTF-8 BOM prefix, an alltogether invalid charset in strings, or simply an out-of-memory error - deeply nested PHP arrays are expensive, and json_decode itself hold some state data while processing. || Else try a different JSON library; PEARs Services_Json, or up_json_decode from upgradephp. Commented Aug 8, 2012 at 20:10
  • can you show the output of JSON (maybe on pastebin.com) which is not working with json_decode for testing? Commented Aug 8, 2012 at 20:36
  • I'm not sure if I can use other json libraries, since I have no control on the server and have no idea what's installed there. The json can be found here: dropbox.com/s/t0nm040wjfgzq25/wordsDB.json Commented Aug 9, 2012 at 16:11

4 Answers 4

3

In your JSON file change null to "null" and it will solve the problem.

Sign up to request clarification or add additional context in comments.

Comments

3

Check if your file is UTF8 encoded. json_decode works with UTF8 encoded data only.

EDIT: After I saw uploaded JSON data, I did some digging and found that there are 'null' key. Search for:

"exceeding":{"S01E01.html":{"2217":1}},null:{"S01E01.html":

Change that null to be valid property name and json_decode will do the job.

5 Comments

Had the same problem and it turned out to be an encoding issue too.
I get null even when I don't go through the file (Encode and decode right away), and decode works with small jsons, so I believe the file isn't the issue. Thanks anyway.
Any way to check what is the recursion limit?
Sorry, wrong copy/paste. Should be nesting limit, which is 20(PHP 5.2.2) and 128(PHP 5.2.3+)
I spent three hours looking for this answer...thank you so much, Boris!
0

I had a similar problem last week. my json was valid according to jsonlint.com.

My json string contained a # and a & and those two made json_decode fail and return null.

by using var_dump(json_decode($myvar)) which stops right where it fails I managed to figure out where the problem was coming from.

I suggest var_dumping and using find dunction to look for these king of characters.

Comments

0

Just on the off chance.. and more for anyone hitting this thread rather than the OP's issue...I missed the following, someone had htmlentities($json) way above me in the call stack. Just ensure you haven't been bitten by the same and check the html source.

Kickself #124

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.