patricia
patricia

Reputation: 1103

PHP json_decode gives empty array

Hi I'm trying to decode a string with json format into a associative array. The string is this one: (My string is from a database and its generated there)

{ 
  "Parameter1":"<style>
                  #label-9 {
                  display: block;
                  text-align: left;
                  color: #fff;
                  }
                </style>", 
  "HistoryPosition":"1"
}

And when I do json_decode() it gives me an empty array. Do you know why this happens? I believe it's something from the "Parameter1" but can't find what it is.

Thank you :)

Upvotes: 0

Views: 1191

Answers (3)

MonkeyZeus
MonkeyZeus

Reputation: 20737

Instead of handwriting your own JSON string, you should absolutely be using PHP's built-in functions to make your like at least 483% easier:

// Use a native PHP array to store your data; it will preserve the new lines
$input = [
    "Parameter1" => "<style>
                  #label-9 {
                  display: block;
                  text-align: left;
                  color: #fff;
                  }
                </style>",
    "HistoryPosition" => "1"
];

// This function will preserve everything in your strings
$encoded_value = json_encode($input);

// See your properly formatted JSON string
echo $encoded_value.'<br><br>';

// Decode the string back into an associative PHP array
echo '<pre>';
print_r(json_decode($encoded_value, true));
echo '</pre>';

Update per new info about DB retrieval

json_last_error_msg(); produces this error:

Control character error, possibly incorrectly encoded

If you do not care about preserving newlines then this will work:

<?php
$db_data = '{ 
  "Parameter1":"<style>
                  #label-9 {
                  display: block;
                  text-align: left;
                  color: #fff;
                  }
                </style>", 
  "HistoryPosition":"1"
}';

$db_data = str_replace("\r", "", $db_data);
$db_data = str_replace("\n", "", $db_data);

echo '<pre>';
print_r(json_decode($db_data, true));
echo '</pre>';

Upvotes: 1

Machavity
Machavity

Reputation: 31614

JSONLint indicates the JSON is invalid.

What you might want to do is the following

$json = '{ 
  "Parameter1":"<style>
                  #label-9 {
                  display: block;
                  text-align: left;
                  color: #fff;
                  }
                </style>", 
  "HistoryPosition":"1"
}';
// remove the newlines
$clean = str_replace(["\r", "\n"], ['', ''], $json);
var_dump(json_decode($clean));

Demo https://3v4l.org/X6KgM

Upvotes: 1

MicE
MicE

Reputation: 5128

Akshay is indeed correct, it is caused by linebreaks.

<pre><?php

$input = <<<EOD
{
  "Parameter1":"<style>
                  #label-9 {
                  display: block;
                  text-align: left;
                  color: #fff;
                  }
                </style>",
  "HistoryPosition":"1"
}
EOD;

// json_decode($input, true);
// echo json_last_error_msg(); // Syntax error

$input = str_replace("\r", null, $input);
$input = str_replace("\n", null, $input);

var_dump(json_decode($input, true));

Prints:

array(2) {
  ["Parameter1"]=> string(176) "<style>                  #label-9 {                  display: block;                  text-align: left;                  color: #fff;                  }                </style>"
  ["HistoryPosition"]=> string(1) "1"
}

Upvotes: 1

Related Questions