Hi,
Below is the data format which I am passing to php via javascript in json format.

{"name":"Secret","value": "checked","name":"OldSpice","value": "unchecked","name":"Degree","value": "unchecked","name":"Dove","value": "unchecked","name":"Axe","value": "unchecked","name":"Maxim","value": "unchecked","name":"Nivea","value": "unchecked",}

I can access the data successfully using the code

echo $_POST['name'];

I want to convert it to an array. I tried to use json_decode() and below is my code but I am getting the below error and I don't know what to do.

$obj = json_decode($_POST);
echo $obj;
Warning: json_decode() expects parameter 1 to be string, array given

Any help will be highly appreciated.

Cheers,
Vishal

Dani AI

Generated

A few practical points to get this working for .

json_decode needs a JSON string — not the entire $_POST array. Send the JSON as one POST field (or as raw JSON) and decode that field. Also make the JSON valid and structured so every item is preserved (remove trailing commas and avoid flat, repeated name/value pairs). Two common, easy-to-decode shapes are:

{"Secret":"checked","OldSpice":"unchecked","Degree":"unchecked"}

or

[{"name":"Secret","value":"checked"},{"name":"OldSpice","value":"unchecked"}]

On the PHP side read and decode only the string, then check for errors:

$json = isset($_POST['payload']) ? $_POST['payload'] : file_get_contents('php://input');
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    die('JSON error: ' . json_last_error_msg());
}

If you used the first shape you get an associative array where keys are product names; if you used the second shape you get a numeric array of objects (each with name and value) — iterate accordingly.

Troubleshooting checklist:

  • Inspect what PHP actually received with var_dump($json) or error_log() before decoding.
  • If you post raw JSON from JS with contentType "application/json" PHP will not populate $_POST; use file_get_contents('php://input').
  • Watch for magic quotes on very old PHP installs (strip slashes if needed) and remove trailing commas from your JSON on the client side.
  • Use json_last_error() / json_last_error_msg() to see why decoding fails.

Good advice from about decoding the real JSON string and removing the trailing comma — follow those and you should be unstuck.

Recommended Answers

All 2 Replies

You don't want to decode the entirety of $_POST. $_POST is, at it says, an array. You want to decode $_POST.

Also, you'll probably want to do a check if magic quotes is on, and strip any slashes if it is.

Finally, I see your JSON has a trailing comma at the end of the contents. If possible, get rid of that trailing comma - IE doesn't like them.

Hi quasipickle,
Appreciate the points mentioned. The comma one was something I did not notice.

Cheers,
Vishal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.