HTMLized API data - javascript

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.

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.

Related

Editing JSON file at multiple positions without rewriting complete file

I have a JSON file that contains lots of nodes. Each node has a unique id. The following is an example skeleton structure of the JSON file.
{
"node1": {
"id": "1",
"name": "student",
"properties": {
"name": "Ram",
"lastname": "Kumar",
"age": "20"
}
},
"node2": {
"id": "2",
"name": "Teacher",
"properties": {
"name": "Ram",
"subject": "Computers"
}
}
Now, this was just sample data. Suppose, I have to find node 2, provided I have the position of the node in the file. That is it's beginning and the end. I create a read stream from the start position and the end position of the file to get the object within the complete JSON object in the file. Now, using JSON.parse function I am able to create an object within the program after parsing it.
But, now I use a writestream to make the changes in the object at the required positions after computing them.
For example, I have to edit the subject property of the teacher, and the main rule is I cannot completely write the object again. Rather, create a writestream at the position where there is subject property and just edit that value.
So, the question is how to do so? And that to with multiple positions to edit, that is editing multiple properties, without rewriting them all.
I know I might face an issue if I use multi-threading for this to simultaneously edit the multiple properties at once, but I need a work around that too.

Replace static json data in handlebars

I'm doing some maintenance on a project I haven't worked with before that uses .hbs files for templating and a static json file for initial data. The javascript appears to be vanilla (no backbone.js, Angular, etc.)
Is there a "correct" way of overwriting the data in that file after I get a JSON object from a REST call AFTER the page loads?
This is what is in the .hbs file:
{{> dropdown-module platform-demos.registerDemoType }}
This is what is in the static JSON file:
"registerDemoType": {
"label": "Demo Type",
"placeholder": "Choose Demo",
"inverted": "inverted",
"id": "demoId",
"items": [{
"option": "Basic Demo",
"optionValue": "basic-demo-dp"
}, {
"option": "Intermediate Demo",
"optionValue": "intermediate-demo-dp"
}, {
"option": "Advanced Demo",
"optionValue": "advanced-demo-dp"
}]
}
And then my REST call pulls back data similar to that:
"demoTypes": [{
"option": "New Demo 1",
"optionValue": "basic-demo-dp"
}, {
"option": "New Demo 2",
"optionValue": "intermediate-demo-dp"
}, {
"option": "New Demo 3",
"optionValue": "advanced-demo-dp"
}]
Is there a "correct" way of replacing the static data with the REST call data?
I feel like you have a fundamental misunderstanding of how handlebars works. Handlebars does not have any form of data binding built into it so you cannot simply change the values that were passed in and have the rendered HTML change automatically.
Instead, when your REST call is complete, you will have to update the object you are passing into your template as Hoyen suggested
registerDemoType.items = demoTypes;
And then you have to re-render this handlebars template, passing in registerDemoType as your data.
In short: You cannot access the hbs data after it has been rendered, you must re-render.
If you just want to replace it. Then all you should really need to do is something like this after the REST call responds:
registerDemoType.items = demoTypes;

Angularjs: 'SyntaxError: Unexpected token , ' when loading object

I'm trying to use Symphony CMS to output its XSL as JSON, for use in the angular phonecat app example from the tutorial. My list view json is output as an array with [{ }] brackets around it. My detail view json is output with only { } around it:
{
"position": 1,
"order": 3,
"total": "1",
"id": "sunt",
"sym_id": "21",
"cat": "back-end",
"imageUrl": "http://localhost:8080/workspace/images/phones/Nondell-streak-7.0.jpg",
"name": "Sunt",
"done": "No",
"priority": "medium",
"date-created" : "18 November 2015",
"date-modified" : "19/11/15 10:41am",
"date-due" : "18/11/15 2:13pm",
"snippet": "Quam nihil molestiae"
}
When loading my page the list view loads. When I click on an item the detail view tries to load but I get: SyntaxError: Unexpected token ,. Why? The syntax seems normal to me?
A big problem I am struggling with is that my json is created dynamically with the CMS but no actual files are on the server as far as I know.
Before I was getting the error: Error in resource configuration for action get. Expected response to contain an object but got an array so I made some changes so the list is output as an array, and the single views are output as an object.
My json is generated on localhost:8080/json/{parameter}. The param is 'todos' when not set. So my list view is situated at localhost:8080/json/todos. The param takes the selected object's name, so my detail view would be at localhost:8080/json/foo when I press the link generated by angular.
I hope my question is complete and makes sense :D

How do I dynamically update a Maps Engine data table with JavaScript?

I have a Maps Engine map with one of the layers linked to a data table containing a single location. I would like to update the data table via JavaScript in Google Sites. The mapsengine.tables.features.batchPatch seems to do what I want, and the help page for the batchInsert version of the same command seems like it could be modified to do what I want. However, I'm having difficulty getting it to work properly. I believe the problem is due to the fact that I don't know what the primary key is for this table or where I can find it (see here for more explanation).
Can anyone here tell me if I'm headed in the right direction, and how I might be able to find this primary key (it also seems to be referred to as a gx_id at times)? Thanks in advance for whatever help you may be able to provide.
Edit: When I go here and get the information on my table, I get the following response:
{
"tables": [
{
"id": {My Table ID},
"etag": "\"6030101253664097613\"",
"projectId": {My Project ID},
"name": "Current Location",
"description": "",
"tags": [
],
"writersCanEditPermissions": false,
"sourceEncoding": "UTF-8",
"processingStatus": "complete",
"bbox": [
-180,
-90,
180,
90
],
"creationTime": "2014-11-11T21:33:43.982Z",
"lastModifiedTime": "2014-11-12T20:55:20.613Z"
}
]
}
As you can see, there is no gx_id listed. Is there some way to add it, or do I need to recreate the table to be able to add it from the start? If I need to recreate the table, what do I need to do to make sure the gx_id is there, since it wasn't immediately apparent when I created the table last time that anything was missing.
Yes, that should work fine. You can find your gx_id simply by fetching a copy of your table and checking the properties. Here's a sample from the docs:
Request:
https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?maxResults=500&version=published&key=(YOUR_KEY_HERE)
Response:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
149.23531999999997,
-35.352484
]
},
"properties": {
...
"gx_id": "1" <-- HERE
}
},

What is wrong with my object(regarding knockoutjs mapping plugin)

I'm pulling my hair out with this. I'm not sure what is going on. I've read all the tutorials on mapping but I'm obviously missing something.
{
"address": "110",
"city": "Durham",
"id": 1,
"name": "Keep",
"persistent": true,
"salesRep": "Me",
"state": "NC",
"user": {
"email": "test#test.com",
"id": 4,
"name": "Test",
"password": "test",
"persistent": true
}
}
I've tried
ko.mapping.fromJSON(data);
and
ko.mapping.fromJS(data);
In my old code I ended up doing this.
viewModel.customers(data);
But my JSON looked different. It didn't have a nested object and it also had brackets on the beginning and end making it an array. It seems this would be pretty basic. I'm not getting any errors at all. Thanks for the help.
EDIT
http://jsfiddle.net/gjemN/
In your sample you are dealing with a JavaScript object and not JSON (string representation of it).
So, you would want to call ko.mapping.fromJS. If you are getting back an array of customers, then you could do:
ko.mapping.fromJS(data2, null, viewModel.customers)
Something like: http://jsfiddle.net/rniemeyer/BQe2z/

Categories