add html tag to json string - html

Is there a way like in xml to add a html tag to json string data.
Example:
{"title": "bla bla", "copy": "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium"}
In my example I use an a tag to wrap some text. In xml I could just use a ctata tag to wrap the entire text.
UPDATE:
Ok so from some of your answers it looks like Json is not the route to go for data that will have html tags in it. What would be a better solution. My html pages have some simple FAQ links. When the user clicks them it will load the approperate data which would be the title and the description. All I'm trying to do is simply have a user click some FAQ titles and then it will show a popup that will be sent the page ID which I was using as my json IDS. Right now it works great with the json data but I can't add html links to the description data.
For example:
{
"intro": [
{"title": "title text", "description": "description text1"},
{"title": "title text", "description": "description text1"}
]
}
In the above example, intro is the page ID. So it can grab this array and find its appropriate title and description. After I did this I noticed some of the copy in the descriptions would have html links in them. Can anyone let me know what would be a better solution to how I setup my external data.

You could call the Javascript escape() function before inserting into your JSON. Then call unescape() on the other end.
escape() on W3Schools website

JSON is just a string, so a simple string manipulation would do the trick. You'd have to be VERY careful to not introduce any syntax errors, however. Simply adding <a href="index.php"> would break the string, as the " are JSON metacharacters. you'd have to insert <a href=\"index.php\">, and probably even double-escape those quotes, depending on how you're doing things.

If you're doing your own JSON serialization (and in most contexts you probably don't want to, but that depends on which langauge you're using), you should start by being familiar with the grammar presented on http://www.json.org/. It tells you right on the front page that double quotes in strings can be escaped with backslashes as \".

I know I'm a bit late to the party, but this is what did it for me! The "render" function takes my data in the JSON key "data" and returns the data in "dataname" as well as my desired html tag (span).
{
"data": "dataname", render: function (data) {
return data + '<span></span>';
}
},
"Other data keys to follow"
{
},

If you are trying to put a link in a json string all you need to do is write your link normally as you would in an html but instead of enclosing the href in double quote use single quote. For example
{"name": "<a href='talk.php'>Talk</a>"}

Related

Postman - How to pass a global variable into JSON body

I'm working with Postman right now and I have to do a lot of requests, and in the body I'm passing a JSON content. I would like to know if is there a way to pass the value of a global variable into JSON body. Thank you.
If using a raw JSON body:
{
"test key":"{{global variable}}"
}
On sending the request, the curly braces will be replaced with the value of the variable.
I think what you want to do is described here.
To use a variable you need to enclose the variable name with double curly braces – {{my_variable_name}}.
Double curly bracket is working in header parameter, url or inside JSON body. Inside tests you should use globals example: {"url": globals.url} or {"url": globals["url"]}
You can pass
{
"productId": {{**ProductID**}},
"quantity": 1
}
Where ProductID is your global variable name
in raw format JSON (application/json)
Yep, double curly brackets is the way to go to achieve this - make sure you have the latest version of Postman (though if you are still running 2014 code when this feature was introduced, shame on you!)
eg:
{
"variable": "{{value}}"
}
See the second paragraph here in the Variables section of the documentation - it specifically mentions the request body.

Rendering html numbers and escaped characters with angular js

i'm using an ng-repeat directive to show a list of posts,
retrieved through a wordpress-json-api.
i have problems with some characters, as the output of wp-json-api escapes some of them.
myjson :
{
"id": 1,
"type": "mynicetype",
"slug": "myniceslug",
"title": "It’s a strange char" //my apostrophe got changed
... }
and my markup:
<li ng-repeat="post in menu.posts | filter:searchText | filter:query" >
<h4 >name : {{post.title}}</h4>
</li>
get rendered (obviously-awfully) as
name : It’s a strange char
What's the best approach for formatting these html numbers? (before assigning the json.success data to the scope or, more easily, on the fly, inside template? but how?)
(btw i don't get the reason why an apostrophe should receive a special treatment by a json-api, but i could really be misunderstanding something here)
Thanks for any clarification

In XML, is there a way to turn all of a node's children into a single string including the nested XML tags?

I have bits like the following in an XML file that is a data source for an HTML page that uses CSS and javascript only. The special XML codes are my own, and I want to process them with javascript.
<listitem>regular text could be in here</listitem>
<listitem>possibly with <b>HTML markup</b></listitem>
<listitem>or <special>special xml</special></listitem>
What I dream of is a way to get from .getElementsByTagName("listitem") to the following array.
["regular text could be in here", "possibly with <b>HTML markup</b>", "or <special>special xml</special>"]
That way, I could process each listitem as part of the array. However, the XML parser breaks apart all the XML for each listitem. Other than using CDATA, which gets messy, is there another way?
I think the answer is:
Array.prototype.slice.call(document.getElementsByTagName("listitem")).map(function(x) {return x.innerHTML})
It will return:
["regular text could be in here", "possibly with <b>HTML markup</b>", "or <special>special xml</special>"]

JSON string data display with breaks?

So I'm creating a game for work, which grabs question and answer data from a JSON file that someone helped me create. All I want to figure out, is how to make the string that is returned from the data below display with line breaks for each of the multiple choice answers:
{
"question": "1",
"text" : "How many times has the Actuarial computer lab been moved? A. Once B. Twice C. Six times D. Fifteen times",
"answer" : "1,1"
},
I've been googling for a while (lots of Stackoverflow threads) but every solution appears to be something different or slightly more complex than what I want to do.
Here is how I'm displaying the string into my div:
var displayDiv = $("#textdisplay");
displayDiv.text(question.text);
You probably want to add <br/> or <p> tags to your text in the JSON file itself. If you've got a text editor that can do regex string replacement, then you want to do something like
Find: ([ABCD]\.\s)
Replace: <br/>$1
Or if you're on a machine with sed, you can use
sed -E 's/([ABCD]\. )/<br\/>\1/g' test.txt
That'd be my recommendation, at least. If you can't change the JSON file, use the same Regex to add breaks in JavaScript, like so:
question.text.replace(/([ABCD]\. )/g, '<br/>$1')

Structured text in JSON

I have been looking for a way to capture structured text (sections, paragraphs, emphasis, lists, etc.) in JSON, but I haven't found anything yet. Any suggestions? (Markdown crossed my mind, but there might be something better out there.)
How about something like this:
[ { "heading": "Foobar Example" },
{ "paragraph":
[
"This is normal text, followed by... ",
{ "bold": "some bold text" },
"etc."
]
}
]
That is:
use a string for plain text without formatting or other mark-up;
use an array whenever you want to indicate an ordered sequence of certain text elements;
use an object where the key indicates the mark-up and the value the text element to which the formatting is applied.
HTML is a well-established way to describe structured text, in a plain-text format(!). Markdown (as you mentioned) would work as well.
My view is that your best bet is probably going to be using some sort of plain-text markup such as those choices, and place your text in a single JSON string variable. Depending on your application, it may make sense to have an array of sections, containing an array of paragraphs, containing an array of normal/bold/list sections etc. However, in the general case I think good old-fashioned blocks are markup will ironically be cleaner and more scalable, due to the ease of passing them around, and the well-developed libraries for full-blown parsing if/when required.
There also seems to be a specification that might accomplish this Markdown Syntax for Object Notation (MSON)
Not sure if for you it's worth the trouble of implementing the spec, but it seems to be an option.