Add a <a> tag with href and target="_black" for opening the link in new tab and use split to remove the href from json.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<table id="userdata" border="5">
<th>Revision Date</th>
<th>Document Name</th>
<th>Department </th>
<th>Description</th>
<th>Link</th>
</table>
<script>
var data = {
"person": [{
"revisiondate": "21 April 2016",
"documentname": "1658MC",
"department": "Sales",
"description": "Available",
"link": "href=1658MC.pdf"
}, {
"revisiondate": "16 April 2016",
"documentname": "VCX16B",
"department": "Enginnering",
"description": "Not Available",
"link": "href=VCX16B.pdf"
}, {
"revisiondate": "15 March 2016",
"documentname": "AB36F",
"department": "Custumer Services",
"description": "Not Available",
"link": "href=AB36F.pdf"
}, {
"revisiondate": "12 Agust 2016",
"documentname": "FC25D",
"department": "Technical Support",
"description": "Not Available",
"link": "href=FC25D.pdf"
}]
}
//$.getJSON("new4.json", function(data) {
// console.log(data);
//$.getJSON('new4.json', function(data) {
$.each(data.person, function(i, person) {
var tblRow = "<tr><td>" + person.revisiondate +
"</td><td>" + person.documentname +
"</td><td>" + person.department +
"</td><td>" + person.description +
"</td><td><a target='_blank' href='"+ person.link.split('href=')[1]+"' >"+person.link.split('href=')[1]+"</a></td></tr>"
$(tblRow).appendTo("#userdata tbody");
});
//});
</script>
</body>
</html>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from Bhuwan on Stack OverflowAdd a <a> tag with href and target="_black" for opening the link in new tab and use split to remove the href from json.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<table id="userdata" border="5">
<th>Revision Date</th>
<th>Document Name</th>
<th>Department </th>
<th>Description</th>
<th>Link</th>
</table>
<script>
var data = {
"person": [{
"revisiondate": "21 April 2016",
"documentname": "1658MC",
"department": "Sales",
"description": "Available",
"link": "href=1658MC.pdf"
}, {
"revisiondate": "16 April 2016",
"documentname": "VCX16B",
"department": "Enginnering",
"description": "Not Available",
"link": "href=VCX16B.pdf"
}, {
"revisiondate": "15 March 2016",
"documentname": "AB36F",
"department": "Custumer Services",
"description": "Not Available",
"link": "href=AB36F.pdf"
}, {
"revisiondate": "12 Agust 2016",
"documentname": "FC25D",
"department": "Technical Support",
"description": "Not Available",
"link": "href=FC25D.pdf"
}]
}
//$.getJSON("new4.json", function(data) {
// console.log(data);
//$.getJSON('new4.json', function(data) {
$.each(data.person, function(i, person) {
var tblRow = "<tr><td>" + person.revisiondate +
"</td><td>" + person.documentname +
"</td><td>" + person.department +
"</td><td>" + person.description +
"</td><td><a target='_blank' href='"+ person.link.split('href=')[1]+"' >"+person.link.split('href=')[1]+"</a></td></tr>"
$(tblRow).appendTo("#userdata tbody");
});
//});
</script>
</body>
</html>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
$.each(data.person, function(i, person) {
var tblRow = "<tr><td>" + person.revisiondate +
"</td><td>" + person.documentname +
"</td><td>" + person.department +
"</td><td>" + person.description +
"</td><a href='" + person.link + "'>link text</a><td>" +
"</td></tr>"
$(tblRow).appendTo("#userdata tbody");
});
You should remove the attrribute 'href' from your json
Or you could just add single quotes to your json data links like so
var data = {
"person": [{
"revisiondate": "21 April 2016",
"documentname": "1658MC",
"department": "Sales",
"description": "Available",
"link": "href='1658MC.pdf'"
}, {
"revisiondate": "16 April 2016",
"documentname": "VCX16B",
"department": "Enginnering",
"description": "Not Available",
"link": "href='VCX16B.pdf'"
}, {
"revisiondate": "15 March 2016",
"documentname": "AB36F",
"department": "Custumer Services",
"description": "Not Available",
"link": "href='AB36F.pdf'"
}, {
"revisiondate": "12 Agust 2016",
"documentname": "FC25D",
"department": "Technical Support",
"description": "Not Available",
"link": "href='FC25D.pdf'"
}]
$.each(data.person, function(i, person) {
var tblRow = "<tr><td>" + person.revisiondate +
"</td><td>" + person.documentname +
"</td><td>" + person.department +
"</td><td>" + person.description +
"</td><a " + person.link + ">link text</a><td>" +
"</td></tr>"
$(tblRow).appendTo("#userdata tbody");
});
Formatting URL's in JSON string
How to make a JSON call to an URL? - javascript
JSON url to link - javascript
how do i create a json file url?
Is the URL to JSON converter really free?
What does the JSON output include?
Can I customize the fields in the JSON?
A standard http GET request should do it. Then you can use JSON.parse() to make it into a json object.
function Get(yourUrl){
var Httpreq = new XMLHttpRequest(); // a new request
Httpreq.open("GET",yourUrl,false);
Httpreq.send(null);
return Httpreq.responseText;
}
then
var json_obj = JSON.parse(Get(yourUrl));
console.log("this is the author name: "+json_obj.author_name);
that's basically it
It seems they offer a js option for the format parameter, which will return JSONP. You can retrieve JSONP like so:
function getJSONP(url, success) {
var ud = '_' + +new Date,
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;
window[ud] = function(data) {
head.removeChild(script);
success && success(data);
};
script.src = url.replace('callback=?', 'callback=' + ud);
head.appendChild(script);
}
getJSONP('http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=js&callback=?', function(data){
console.log(data);
});
» npm install json-url
var ajaxRequest = new XMLHttpRequest();
var email= document.getElementById('email').value;
var password= document.getElementById('password').value;
var name= document.getElementById('name').value;
var url = "input.json";
currently, i want the json file to take in user input from javascript, and i have the url as just input.json stored in a variable. how do i get an actual blank json file so that whenever the user inputs data with js, it stores it into the file?
You could create a function which creates the URL based on the object provided.
Destructure the parameter to get
scheme,server,path. Assign a default value topathsince it is optional. Get the remaining unknown properties to arestvariable using spread syntax (Properties likeitem,itemDetailsetc will be inside the rest object)You can create the base URL using the first 3 variables using a template literal
To get the query string, you can use
URLSearchParamsconstructor. It has several overloads. You can pass a query string or an object or an array of entries as parameter to the constructor. If the object is flat, you simply do,new URLSearchParams(rest).toString()to create the query string. But, since you have nested arrays and objects, we need to create an array of entries first (Something like:[[key1, value1], [key2, value2],...and so on]. Then the string will be keykey1=value1&key2=value2. The values will also be properly encoded to replace space and special characters etc)You can create a recursive function to get the nested entries. Loop through the entries of the given object. If the current value is an
Array, recursively call the method for each object in the array and push it to entries. If the value is an object, recursively call the function on the value. This function will return a 2-dimensional array of all the key-value pairs in the object.Then simply pass the entries to
URLSearchParamsto create the query string. If the query string is not empty, append it to the base URL with a prefix of?.
function createURL({ scheme, server, path = '', ...rest }) {
let url = `${scheme}://${server}/${path}`;
let param = new URLSearchParams(getEntries(rest)).toString();
if (param)
url += "?" + param;
return url
}
function getEntries(o = {}) {
const entries = [];
for (const [k, v] of Object.entries(o)) {
if (Array.isArray(v))
entries.push(...v.flatMap(getEntries))
else if (typeof v === 'object')
entries.push(...getEntries(v))
else entries.push([k, v])
}
return entries;
}
console.log(createURL({
scheme: "https",
server: "example.com"
}))
console.log(createURL({
scheme: "https",
server: "example.com",
path: "items",
item: "apple",
"itemDetails": [{
"country": "US",
"year": "2019"
}]
}))
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Maybe is not the best way, but you can try
var myjson = {
scheme:https,
server:example.com
};
var obj = JSON.parse(myjson);
document.getElementById("#div1").innerHTML = obj.scheme + obj.server;