2

I am requesting data from another website and expecting a clean json array in return.

However I am getting this instead:

<pre></pre>{"Status":"Success","Result":1}

which won't parse with json_decode();.

How do I extract the JSON array out of this data so I can parse it?

Note: I am not in control of the code I am requesting the data from.

4
  • 1
    Where is that data coming from? Is that exactly what's returned from the remote server, or is that what you echoed? The question is "where did those HTML tags come from?" If they came from the remote server, then you need to let the administrator know they have a bug in their API. If they are somehow being added by your code, then you know the bug is in your code. Commented May 16, 2014 at 16:04
  • They're from the remote server. Yes I alerted the programmer. Commented May 16, 2014 at 16:06
  • How about simply, str_replace("<pre></pre>", "", $json_string); ? Commented May 16, 2014 at 16:06
  • The contents of the tags are not always empty. It depends on the query. Commented May 16, 2014 at 16:13

6 Answers 6

5

try this

$output_array = array();

$badstr = '<pre></pre>{"Status":"Success","Result":1}';

preg_match("/{.*}/", $badstr, $output_array);

in $output_array[0] you have your json string.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that <pre></pre> is constant, then just a simple substring operation:

$badstr = '<pre></pre>{"Status":"Success","Result":1}';
$goodstr = substr($badstr, 11);

But you really should yell at the server admins for sending out bad json in the first place. There's no excuse for this kind of thing. It's probably some debug code they forgot to take out.

1 Comment

Unforunately it's not alway constant.
1

If you want it to work both now, and once the issue will be fixed, you can do this:

$result = '<pre></pre>{"Status":"Success","Result":1}';

if (strpos($result ,'<pre>') !== false) 
{
    $array = json_decode(substr($result , 11));
}
else
{
    $array = json_decode($result);
}

Comments

1

Only remove <pre></pre>, only if it's the first thing:

$response = preg_replace('#^<pre></pre>#', '', $response);

Comments

0

How about simply string replace?

Like so:

$json_string = '<pre></pre>{"Status":"Success","Result":1}';

$json = str_replace("<pre></pre>", "", $json_string);

echo $json;

Output:

{"Status":"Success","Result":1}

2 Comments

What if the JSON contains a <pre></pre>? Is it encoded or escaped so it won't remove it?
@Rudie, yes, good point. Your preg_replace('#^<pre></pre>#', '', $response); answer is better. upvoted.. :)
0

If you don't expect any html tags in your output, you can also use strip_tags():

$not_json = '<pre></pre>{"Status":"Success","Result":1}';
$json_string = strip_tags($json);
$result = json_decode($json_string);

6 Comments

That's a coincedental fix. Don't use it because it works right now. Maybe there will be HTML tomorrow.
@Rudie Of course, but it's not clear what the added characters are / can be nor what the json can contain so that really applies to all answers.
If it would strip everything from inside the tags too it would work.
@BenA.Noone You need to be more specific about what the data can and cannot contain as it is just guessing now.
Between the pre tags? text and html, but not likely an array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.