Finding or generating correct filename extensions - html

Say I have a JSON Array called 'blogPost' like below:
[
{
"date" : "2020.11.25",
"title" : "Getting help on Stack overflow"
},
{
"date" : "2020.11.18",
"title" : "Putting my site together"
},
...
]
and a folder of images with the post date like below:
2020.11.25.jpg, 2020.11.18.png, 2020.11.03.gif, etc ...
I was hoping to bring in, edit, and .append() a template html snippet using my JSON array - the "date" would populate both some text and a file path in the html snippet:
<div>
<img src="blog/images/{date}.{extension}" alt="{date}">
<h5>{date} - {title}</h5>
</div>
Jquery:
$.getJSON( "blog/posts.json", function(data) {
$.each(data, function(index,item) {
let extension = 'jpg' || 'png' || 'gif';
$("<div>").load("blog/blog-snippet.html", function() {
$(this).html($(this).html().replaceAll("{date}", item.date));
$(this).html($(this).html().replaceAll("{title}", item.title));
$(this).html($(this).html().replaceAll("{extension}", extension));
$("#content").append($(this).html());
});
});
});
The idea was to use "date" from the array multiple times just to slim and automate the process.
I can change the name of each text and image file using the looped array. However is there a way to generate the correct file extension (jpg, png, etc...) for each image file using html or js/jquery? My current extension variable doesn't find the "correct" extension - it just uses jpeg. This results in only some of the correct file paths.
I suppose my other option would be to add an "image" option to the JSON Array and include the correct file extension.
Any advice is welcome!

Related

Sending multiple files in Postman - NodeJS

I have a JSON request in the following structure-
{
"user_id" : "1",
"user_details" : {
"name" : "my_name"
"passport_image" : "passport_image.jpg"
},
"user_documents" : [
{"file" : "doc_1.jpg"},
{"file" : "doc_2.jpg"}
]
}
How can I send files via postman that are part of a JSON request?
This is what I tried -
But then passport_image.jpg would be its own field and not part of user_details object, correct?
And what about the array of file objects under user_documents? How can I send it too in the request?
I would appreciate help as I'm quite new to using form-data requests rather than raw JSON ones.
You can set the keys as array. It can be set like this. user_documents[0] and the respective file for it.
This will work!
The above answer is wrong, the way to do it is to select the key value = to the value you used in your code, for instance see below:
const storage = multer.diskStorage({
destination: function(req,file,cb) {
cb(null,'uploads/')
},
filename: function(res,file,cb) {
console.log('file= ',file)
const uniqueSuffix=Date.now()+'-'+Math.round(Math.random()*1e9)
cb(null, file.fieldname+'-'+uniqueSuffix+'.jpg')
}
})
const upload = multer({storage:storage})
app.post('/uploads', upload.array('files',12), (req,res)=>
{
req.files.map((file)=>{
console.log('received request: ', file)
})
res.status(200).send('received files')
})
Then in postman you just select several files, in this case less than 12 after clicking on the select files button. Just one entrance for multiple files.
Actually I find solution on Youtube. You can choose multiple files by keeping Ctrl and choose multiple files.

response with status 200: ok

I am trying to figure out how to plot data from a local '.JSON' file using angular2-highcharts example.
I followed the example in 'https://www.npmjs.com/package/angular2-highcharts' to first understand how to plot .JSON data and it worked. I took the data available for the example and created a local .JSON file (copied the content from 'https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK' in notepad and saved it with UTF-8 encoding as a .JSON file), and replaced the file path for the JSON request to this. When I do this though, I get an error - response with status 200.
constructor(jsonp : Jsonp) {
//jsonp.get('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK').subscribe(res => {
jsonp.get('./data.json').subscribe(res => {
this.options = {
title : { text : 'AAPL Stock Price' },
series : [{
name : 'AAPL',
data : res.json(),
tooltip: {
valueDecimals: 2
}
}]
};
});
}
options: Object;
};
Since I am not super familiar with json data/ Javascript or angular2 I am not sure if I am missing something very basic here. Any help is appreciated.
as far as I know, Response Status 200 specifies that request was successful. i.e. your request was successfully handled. perhaps you want to try checking response data.
check your callback for response data.
Using http instead of json helped. I made use of the suggestion in this answer https://stackoverflow.com/a/36305814/4567096.

highstock with json data

I want use highstock to show my data in the web ,but i have someproblem with the json data,
the Example of basic-line :http://www.highcharts.com/stock/demo/basic-line
http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/stock/demo/basic-line/
i change the javascript file the
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data)
to the
$.getJSON('data.json', function(data)
i want use the json data in my web folder and i put data in the json file like :
[[1143072000000,60.16],
[1143158400000,59.96],
[1143417600000,59.51],
[1143504000000,58.71],
[1143590400000,62.33],
[1143676800000,62.75],
[1143763200000,62.72],
/* Apr 2006 */
[1144022400000,62.65],
[1144108800000,61.17],
[1144195200000,67.21],
[1144281600000,71.24]]
but i can see nothing in my web, what is wrong with me? is it the json data Format wrong?or others thanks for give me some help
In case when you use JSON, then you should get rid of all comments.
So JSON should looks like:
[[1143072000000,60.16],
[1143158400000,59.96],
[1143417600000,59.51],
[1143504000000,58.71],
[1143590400000,62.33],
[1143676800000,62.75],
[1143763200000,62.72],
[1144022400000,62.65],
[1144108800000,61.17],
[1144195200000,67.21],
[1144281600000,71.24]]
and chart code:
$.getJSON('data.json', function(data) {
window.chart = new Highcharts.StockChart({
chart : {
renderTo : 'container'
},
rangeSelector : {
selected : 1
},
series : [{
name : 'AAPL',
data : data
}]
});
});
Rename your file to data.js . For javascript file and not JSON. It's like that for me :)

d3js forced directed cannot read from json

I have a simple json file which is :
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":35,"target":44,"value":1},
{"source":44,"target":35,"value":1},
{"source":45,"target":35,"value":1},
{"source":45,"target":44,"value":1},
{"source":35,"target":49,"value":1},
{"source":49,"target":35,"value":1}
]
}
when I save it use exactly the html code as shown in http://bl.ocks.org/4062045#index.html and address the above json, nothing appears on the cancas.
I appreciate it if you help me with this one as I am not very familiar with it. Moreover, it would be great if I know the minimum code required for drawing a graph like this using json.
Best,
The number of "source" and "target" refer to the index of the item in nodes array.
So you can change your json to following:
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":0,"target":1,"value":1},
{"source":1,"target":2,"value":1},
{"source":2,"target":3,"value":1},
{"source":3,"target":4,"value":1},
]
}
Then you can just copy the codes from http://bl.ocks.org/4062045#index.html example as the minimum code.
Remenber to change the json file to your own json file.
d3.json("path/to/your/json", function(error, graph) {
//codes
});

working with JSON data, trying to parse nested data

I just working with JSON data and am playing around with jQuery and Ajax requests. Pretty basic stuff, but here's my problem.
I have a basic data set which I was using for time tracking. I know how to parse the simple JSON data like this:
{
"end" : "1/18/2011",
"start" : "1/18/2011",
"task" : "Code Review",
},
It's the more complicated stuff I'm trying to parse like this where I'm trying to pull the "time" data out.
{
"end" : "1/17/2011",
"start" : "1/17/2011",
"task" : "Exclusive Brands",
"time" : {
"analysis" : 4,
"documentation" : 3,
"meetings" : 2
}
This is the code for the script I've been using to parse the simple data:
$(function() {
$('.load').click(function(){
$.getJSON("data.js",function(data){
$.each(data.timesheet, function(i,data){
var div_data ="<div class='box'>"+data.start+" "+data.task+"</div>";
$(div_data).appendTo("#time-tracking");
});
}
);
return false;
});
});
My question is what's the format to parse the time data, or what's the best way to parse the information nested inside the time element?
Any help will be greatly appreciated.
A JSON string will be parsed into an object. When parsed, the time is the key of one object. You could retrieve the value of this object through the dot operator (.).
data = JSON.parse('{"end":"1/17/2011", "start":"1/17/2011", "task":"Exclusive Brands", "time": {"analysis":4, "documentation":3, "meetings":2 } }')
// => obj
data.time.analysis
// => 4
In your case similarly you could use the data.time.meetings to access your data from remote server.
Unless I am terribly mistaken, since jquery already converted data into a javascript for you, you should be able to access time as if it was a javascript object like so:
var analysis = data.time.analysis;
var documentation = data.time.documentation;
var meetings = data.time.meetings;
etc...