0

I have a web app that accesses data from an API. I need to pull data out, but it's been 'HTMLized' and I'm unsure how to go about rendering the actual data.

I've tried pulling the data from the array using bracket notation, which gives me the data but it includes the HTML tags.

javascript

{
"id": "5c9cd6576ebf251b944ed16d",
"number": 3451,
"user_id": "5b8425c8e694aa3c6a835b67",
"state": "active",
"subject": "Bower Steel 3D",
"label_ids": [
    "5c4b0cf4bbddbd48cd69e68a"
],
"customer_id": "5b51be9ee694aa03f8c834be",
"type": "email",
"reply_to": "xxxxxxx.xxxxxx@xxxxxxx.com",
"reply_cc": "",
"group_id": "5a5f65fed5593070120b9779",
"inbox_id": "5a5f65fed5593070120b9779",
"updated_at": "2019-04-03T12:46:50Z",
"created_at": "2019-03-28T14:12:39Z",
"spam": false,
"trash": false,
"summary": "Great! Glad to hear it.",
"rating": null,
"waiting_since": "2019-04-03T12:21:43Z",
"messages": [
    {
        "id": "5c9cd6576ebf251b944ed16e",
        "type": "reply",
        "from_name": "Cxxxxxxx Sxxxxxx",
        "body": "<div class=\"5cb74b836ebf2578174d567c\">        
                 <style>cb74b836ebf2578174d567c
                         p.5cb74b836ebf2578174d567cMsoNormal,
                 </style>\n\n\n
                 <div class=\"5cb74b836ebf2578174d567cWordSection1\">\n
                 <p class=\"5cb74b836ebf2578174d567cMsoNormal\">
                   Actual content I want to render
                 </p>
                 <p class=\"5cb74b836ebf2578174d567cMsoNormal\"></p>\n
                 <p class=\"5cb74b836ebf2578174d567cMsoNormal\"></p>\n
                 <p class=\"5cb74b836ebf2578174d567cMsoNormal\"></p>

The content I want to render is embedded like this. It's basically an array within the object, called messages, and it contains a message trail - from the 1st message to the last reply.

I've never come across HTMLized data like this. I can get to the data by using bracket notation, but I don't know how to parse the extraneous tags and get to the data.

Any help would be appreciated.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Is the API a third party one? – Francinaldo Almeida Apr 17 '19 at 16:11
  • The answer here kinda depends on the nature of the data. You could simply `strip_tags` on the example you've given, but that may not be a case for the real data. – ceejayoz Apr 17 '19 at 16:15
  • convert html string to dom methods https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518 – imvain2 Apr 17 '19 at 16:33

1 Answers1

0

Short of having the API produce a better endpoint for you to consume, you will have to handle the 'HTMLized' data. In cases like this, I have used something to parse the HTML. For example you could throw the body string into the jQuery constructor.

jQuery(messages[i].body)

That will give you something digestible programmatically to search for your data.

DKB
  • 1