Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to strip invalid characters from JSON output in PHP
    text
    copied!<p>I am a PHP developer working on code that will step through and parse a JSON object so that I can manipulate data before outputting it in its final form. Here's some example JSON (its very large so I am removing most of the items I need and just putting the invalid part):</p> <pre><code>[ { "items": [ { // this is valid and what I need. } ], "espots": [ { "content": " [TopNavigation_Appliances] " } ] }, </code></pre> <p>I have effectively stripped the JSON from its source to be strictly the JSON data that's being passed to json_decode(). However, I am always getting a null result because the JSON object has an invalid part that my code doesn't actually require to function. The whole "espots" part of the JSON is unneeded within my application, but the multiple line "content" part is screwing json_decode(). Is there a way in PHP via RegEx or some nifty PHP that will strip out these items from the JSON output so that I can properly use json_decode() to retrieve n array that my code can then manipulate and deal with in the way I need it to?</p> <p>Here is the error message given by JSONLint (and one other validator):</p> <pre><code>Parse error on line 263: ... "content": "[TopNavigation_App -----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[' </code></pre> <p>I'm gathering that "[ is causing invalid JSON, but I'm no expert on Json.</p> <p>I've attempted the following to no avail:</p> <ul> <li>str_replace() on the lines in its original format</li> <li>trim() and str_replace</li> <li>trim()</li> <li>str_replace removing the line breaks then attempting to str_replace() the culprit parts of the JSON.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload