1

I am using php json_decode to parse API response and then json_encode to send response to client.

In a third party API call I am getting JSON response in which there is a key with value having html tags and html contents. When I print that data I can see those tags, but after json_decode, I can not see those tags, so that I am unable to keep those tags intact. I want that html content to go to database as it is [without any change at all].

$result = curlGetWithHeaders("API URL", "Parameters for that URL");

$data = json_decode($result);

So in $result I can see html tags but not in $data. And var_dump($data) says that it is string.

Response is = {"array":{"id":1,"title":"Title","short_title":"abcd","description":"<ul><li>0</li><li>A</li><li>B</li><li>C</li><li>D</li>"}}

and after using json_decode I am getting description as pasred html.

deals] => Array ( [0] => stdClass Object ( [deal] => stdClass Object ( [id] => 1 [title] => title [short_title] => abcd [description] => 0 • A • B • C

5
  • near duplicate of this question. The selected answer there not so stellar though, IMHO. Commented Jan 11, 2014 at 5:40
  • thanks for the reply, it means I should use str_replace to replace < with &lt; and > with &gt; Commented Jan 11, 2014 at 5:44
  • Can you show an example of what happens? Commented Jan 11, 2014 at 5:45
  • The title says the question is about json_encode but only json_decode is asked about. Which is it now? Commented Jan 11, 2014 at 5:51
  • apologies for all. code added, subject changed. Commented Jan 11, 2014 at 5:53

5 Answers 5

5

When you are using print_r to display the contents of your JSON-decoded object, what you see in your browser is something like that:

stdClass Object ( [array] => stdClass Object ( [id] => 1 [title] => Title [short_title] => abcd [description] =>

  • 0
  • A
  • B
  • C
  • D

However, the effective contents of your object (without the browser interpreting the HTML tags) is this:

stdClass Object
(
  [array] => stdClass Object
    (
      [id] => 1
      [title] => Title
      [short_title] => abcd
      [description] => <ul><li>0</li><li>A</li><li>B</li><li>C</li><li>D</li>
    )
)

You can see it clearly if you ask your browser to display the source of the page.

Your description field contains a plain string that happens to be HTML, that's all.

If you want to store the JSON-decoded data in your database without any modification, well, don't do anything and you'll get exactly what you want.

Now if you want to display the actual HTML code in your browser, you will have to escape the HTML special characters, like so:

echo htmlentities (print_r (json_decode ($json), true));

Note that you should get the print_r result first, and only then escape the HTML.

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

14 Comments

Your spot on about this. However I wonder how he outputted $result without the browser parsing the HTML?
As long as the HTML tags are inside a quoted string, they are not interpreted, so a print_r of the JSON data would display the (quoted) HTML code without the browser interpreting it.
I am looking into network request responses for now, using chrome.
@kuroineko : I can see tags in string format when printed before json_decode.
@Pranav You said you wanted to store the returned API data raw in the database without any modification. Well don't to anything and you will get exactly that. If you want to display the HTML to the user without the browser intercepting and parsing it then you will have to encode it before outputting to the browser.
|
1

Not sure if it helps but...

$p = '<p> This is some text</p>';

$j = json_encode(urlencode($p));

print_r($j);

echo '<br>';

$jd = json_decode(urldecode($j));

print_r($jd);

Comments

1
$result = '{"array":{"id":1,"title":"Title","short_title":"abcd","description":"<ul><li>0</li><li>A</li><li>B</li><li>C</li><li>D</li>"}}';
$data = json_decode($result);
print "<pre>";
print_r($data);
print "</pre>";  

Here your json string already got converted in array where "description" is having html tags included. with print_r you will bot able to see html tags as it gets interpreted by browser and you will see result that html will generate.

Now, if you want to shoe text with HTML you can use htmlentities like shown below.

print htmlentities($data->array->description);    

Output:

<ul><li>0</li><li>A</li><li>B</li><li>C</li><li>D</li>

2 Comments

... or you could simply display the page source code. Saves a bit of typing :).
@kuroineko Or even better Xdebug together with var_dump().
1

use htmlspecialchars() function to encode the data first

and use htmlspecialchars_decode() function to get back the result

$result = htmlspecialchars({"array":{"id":1,"title":"Title","short_title":"abcd","description":"<ul><li>0</li><li>A</li><li>B</li><li>C</li><li>D</li>"}});

use it and when want back the html content

htmlspecialchars_decode() to get the html content back

3 Comments

thanks for response, But I get data in API response, so I need to json_decode it first. after htmlspecialchars unable to decode to json as it replaced even double quotes.
ENT_NOQUOTES use this flag
htmlentities($result, ENT_NOQUOTES);
0

Not exactly the answer to my question but it may help someone, whatever I was storing in DB was perfect. Problem was while displaying it. So now I could do it with <pre></pre> tags which will format the data while displaying. It just worked for me.

Comments

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.