NekoLopez NekoLopez - 9 days ago 7
Javascript Question

Get only url value of FileStack JSON.stringify?

I'm using FileStack on my website, according to FileStack Docs I have this actually:

<iframe id="framefile" width="700" height="500" frameborder="0">
<p>Your browser does not support iframes.</p>
</iframe>
<div id="files"></div>


And in javascript side:

<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="http://api.filepicker.io/v2/filepicker.js"></script>

<script type="text/javascript">
filepicker.setKey("myAPIKey");
filepicker.pickMultiple(
{
mimetype: '*/*',
language: 'es_mx',
container: 'framefile',
services: ['COMPUTER','GOOGLE_DRIVE','DROPBOX']
},
function(Blobs){
console.log(JSON.stringify(Blobs));
},
function(FPError){
console.log(FPError.toString());
});
</script>


I need to get only URL files values and put it in a div. I tried this:

<script type="text/javascript">
filepicker.setKey("myAPIKey");
filepicker.pickMultiple(
{
mimetype: '*/*',
language: 'es_mx',
container: 'framefile',
services: ['COMPUTER','GOOGLE_DRIVE','DROPBOX']
},
function(Blobs){
console.log(JSON.stringify(Blobs));
var result = JSON.stringify(Blobs, ['url']);
$('#files').html(result);
},
function(FPError){
console.log(FPError.toString());
});
</script>


I obtain this:


[{"url":"https://cdn.filepicker.io/api/file/--link1--"}]


But How can I obtain URL values but in this way?:


https://cdn.filepicker.io/api/file/--link1--


Anyone have a solution?

Answer

You're almost there, are not you?

What about appending [0].url to the object which is returned ?

I mean substituting result = JSON.stringify(Blobs, ['url']); for result = JSON.stringify(Blobs, ['url'])[0].url;

UPDATE

My first anwser is wrong because result in

result = JSON.stringify(Blobs, ['url'])

returns '[{"url":"https://cdn.filepicker.io/api/file/--link1--"}]', which is a string since it comes from a stringifyed variable. Thus, result[0] returns its first character, which is "[". Finaly result[0].url or put differently, "[".url is undefined. (and can hardly be so)

Replace this part:

function(Blobs){
    console.log(JSON.stringify(Blobs));
    var result = JSON.stringify(Blobs, ['url']);
    $('#files').html(result);
}

by

function(Blobs){
    $('#files').html(Blobs[0].url);
}

If one wants to get the desired information from a json, one must not stringify it.

Update

Or if you want to display numerous urls, because of multiple files replace the latter method by

function(Blobs){
    var displayedUrls = "";
    for (i = 0; i < Blobs.length; i++) { 
        displayedUrls += Blobs[i].url + "<br>";
    }
}
$('#files').html(displayedUrls);