title: json_decode not working on string variable
body: <p>I'm trying to decode a json string into a php array and I'm getting a syntax error (4) when I use json_last_error().</p>
  
  <p>The string comes from an API call to an Infusionsoft database. The database returns an array with one key/value pair. The value contains the json string.</p>
  
  <pre><code>Array ( [_PMSChargeItems] =&gt; [{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}] )
  </code></pre>
  
  <p>When I save the value to a string variable and pass it to json_decode() I get the syntax error (it doesn't work). If I pass the entire json string itself (copy and paste from echoing the string var value) to json_decode() the array is created (it works).</p>
  
  <p>I have validated the json string at both <a href="http://jsonlint.com/" rel="nofollow">http://jsonlint.com/</a> and <a href="http://www.functions-online.com/json_decode.html" rel="nofollow">http://www.functions-online.com/json_decode.html</a>.</p>
  
  <p>How can I get the string to be correctly decoded when it is passed to the json_decode function using a variable?</p>
  
  <p>Here's the code I am using to decode:</p>
  
  <pre><code>$cid = $_GET['Id'];
  $returnFields = array('_PMSChargeItems');
  $conDat = $appConnect-&gt;dsLoad("Contact", $cid, $returnFields);
  
  $a = $conDat['_PMSChargeItems'];
  var_dump(json_decode($a, true));
  
  echo json_last_error();
  </code></pre>
  
  <p>Here's the result of var_dump($a) before the decode:</p>
  
  <pre><code>string(4649) "[{"Date":"2012-09-07T00:00:00","Amount":0.0},{"Date":"2012-09-07T00:00:00","Amount":41.0},{"Date":"2012-09-07T00:00:00","Amount":64.0},{"Date":"2012-09-11T00:00:00","Amount":80.0},{"Date":"2012-09-11T00:00:00","Amount":474.0},{"Date":"2012-09-19T00:00:00","Amount":82.0},{"Date":"2012-09-19T00:00:00","Amount":125.0},{"Date":"2012-09-19T00:00:00","Amount":127.0},{"Date":"2012-09-19T00:00:00","Amount":174.0},{"Date":"2012-09-19T00:00:00","Amount":343.0},{"Date":"2012-09-19T00:00:00","Amount":618.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-19T00:00:00","Amount":1122.0},{"Date":"2012-09-27T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-02T00:00:00","Amount":0.0},{"Date":"2012-10-10T00:00:00","Amount":0.0},{"Date":"2012-11-07T00:00:00","Amount":0.0},{"Date":"2012-11-19T00:00:00","Amount":64.0},{"Date":"2012-12-21T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":0.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":470.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":625.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-01T00:00:00","Amount":679.0},{"Date":"2013-02-13T00:00:00","Amount":0.0},{"Date":"2013-02-18T00:00:00","Amount":0.0},{"Date":"2013-02-25T00:00:00","Amount":65.0},{"Date":"2013-02-25T00:00:00","Amount":85.0},{"Date":"2013-03-11T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-14T00:00:00","Amount":0.0},{"Date":"2013-03-20T00:00:00","Amount":240.0},{"Date":"2013-06-12T00:00:00","Amount":0.0},{"Date":"2013-07-23T00:00:00","Amount":0.0},{"Date":"2013-07-26T00:00:00","Amount":0.0},{"Date":"2013-08-23T00:00:00","Amount":0.0},{"Date":"2013-09-09T00:00:00","Amount":0.0},{"Date":"2014-08-04T00:00:00","Amount":0.0},{"Date":"2014-08-11T00:00:00","Amount":30.0},{"Date":"2014-08-11T00:00:00","Amount":66.0},{"Date":"2014-08-11T00:00:00","Amount":85.0},{"Date":"2014-09-02T00:00:00","Amount":0.0},{"Date":"2014-12-16T00:00:00","Amount":0.0},{"Date":"2015-01-09T00:00:00","Amount":0.0},{"Date":"2015-02-10T00:00:00","Amount":14.0},{"Date":"2015-02-10T00:00:00","Amount":16.0},{"Date":"2015-02-10T00:00:00","Amount":43.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":130.0},{"Date":"2015-02-10T00:00:00","Amount":250.0},{"Date":"2015-02-10T00:00:00","Amount":640.0},{"Date":"2015-02-23T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-02-25T00:00:00","Amount":0.0},{"Date":"2015-03-26T00:00:00","Amount":0.0},{"Date":"2015-05-21T00:00:00","Amount":0.0}]"
  </code></pre>
  
  <p>I've tried to find hidden characters using html_entities and html_entity_decode and didn't find any. I also confirmed using mb_detect_encoding that the string has all ASCII characters.</p>
  
  <p>I just tried the following:</p>
  
  <pre><code>var_dump(json_decode(strval($a), true));
  </code></pre>
  
  <p>The result was NULL and the decode error was the syntax error.</p>
  
  <p>What's curious is that var_dump on ($a) gives me a character count of 4649 whereas strlen('[("Date"....) of the string itself gives a character count of 2789...</p>