how to use JSON.stringify and json_decode() properly

PhpJavascriptArraysJsonStringify

Php Problem Overview


Im trying to pass a mulitidimensional Javascript array to another page on my site by:

  • using JSON.stringify on the array

  • assigning the resultant value to an input field

  • posting that field to the second page

  • using json_decode on the posted value

  • then var_dump to test

  • (echo'ing the posted variable directly just to see if it came through at all)

Javascript on page one:

var JSONstr = JSON.stringify(fullInfoArray);
document.getElementById('JSONfullInfoArray').value= JSONstr;

php on page two:

$data = json_decode($_POST["JSONfullInfoArray"]);
var_dump($data);

echo($_POST["JSONfullInfoArray"]);

The echo works fine but the var_dump returns NULL

What have I done wrong?


This got me fixed up:

$postedData = $_POST["JSONfullInfoArray"];
$tempData = str_replace("\\", "",$postedData);
$cleanData = json_decode($tempData);
var_dump($cleanData);

Im not sure why but the post was coming through with a bunch of "" characters separating each variable in the string

Figured it out using json_last_error() as sugested by Bart which returned JSON_ERROR_SYNTAX

Php Solutions


Solution 1 - Php

When you save some data using JSON.stringify() and then need to read that in php. The following code worked for me.

json_decode( html_entity_decode( stripslashes ($jsonString ) ) );

Thanks to @Thisguyhastwothumbs

Solution 2 - Php

When you use JSON stringify then use html_entity_decode first before json_decode.

$tempData = html_entity_decode($tempData);
$cleanData = json_decode($tempData);

Solution 3 - Php

You'll need to check the contents of $_POST["JSONfullInfoArray"]. If something doesn't parse json_decode will just return null. This isn't very helpful so when null is returned you should check json_last_error() to get more info on what went wrong.

Solution 4 - Php

None of the other answers worked in my case, most likely because the JSON array contained special characters. What fixed it for me:

Javascript (added encodeURIComponent)

var JSONstr = encodeURIComponent(JSON.stringify(fullInfoArray));
document.getElementById('JSONfullInfoArray').value = JSONstr;

PHP (unchanged from the question)

$data = json_decode($_POST["JSONfullInfoArray"]);
var_dump($data);

echo($_POST["JSONfullInfoArray"]);

Both echo and var_dump have been verified to work fine on a sample of more than 2000 user-entered datasets that included a URL field and a long text field, and that were returning NULL on var_dump for a subset that included URLs with the characters ?&#.

Solution 5 - Php

stripslashes(htmlspecialchars(JSON_DATA))

Solution 6 - Php

jsonText = $_REQUEST['myJSON'];

$decodedText = html_entity_decode($jsonText);

$myArray = json_decode($decodedText, true);`

Solution 7 - Php

I don't how this works, but it worked.

$post_data = json_decode(json_encode($_POST['request_key']));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWesley SmithView Question on Stackoverflow
Solution 1 - PhpS. A. MalikView Answer on Stackoverflow
Solution 2 - Phpuser1881928View Answer on Stackoverflow
Solution 3 - PhpBartView Answer on Stackoverflow
Solution 4 - PhpYuvView Answer on Stackoverflow
Solution 5 - PhpLOVEPINView Answer on Stackoverflow
Solution 6 - PhpKRISHNA KANT SINGHView Answer on Stackoverflow
Solution 7 - Phpuser10971804View Answer on Stackoverflow