chand pb
asked on
parse a JSON string
Hello,
I have the following JSON . How do I parse it and get the data value [80,75,3,4,20] as base64 string in javascript
Thanks for your help
{
"contentHeaders" : {
"Content-Disposition" : attachment; filename=\"e.docx\"; filename*=UTF-8''e.docx,
"Content-Type" : application/vnd.openxmlfor mats-offic edocument. wordproces singml.doc ument
},
"data" : {
"type" : Buffer,
"data" : [
80,
75,
3,
4,
20
]
},
"filename" : e.docx
}
I have the following JSON . How do I parse it and get the data value [80,75,3,4,20] as base64 string in javascript
Thanks for your help
{
"contentHeaders" : {
"Content-Disposition" : attachment; filename=\"e.docx\"; filename*=UTF-8''e.docx,
"Content-Type" : application/vnd.openxmlfor
},
"data" : {
"type" : Buffer,
"data" : [
80,
75,
3,
4,
20
]
},
"filename" : e.docx
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you show us how you are getting the data and how you need to send the data. Show us the code that does each.
I have shown you above how to parse the incoming JSON string but I suspect that if you code your request correctly you can have that done automatically for you - but I need to see your code.
The same applies for sending the data out - show us how this needs to be done.
I have shown you above how to parse the incoming JSON string but I suspect that if you code your request correctly you can have that done automatically for you - but I need to see your code.
The same applies for sending the data out - show us how this needs to be done.
ASKER
Thanks.. but I tried parse but it still does not work. getting undefined
REST CALL to download file
function load(req, res) {
filesService.downloadFromS ource(req. params.sou rce, req.body)
.then(function (downloadedFileObj) {
console.log(downloadedFile Obj);
res.status(204).send(downl oadedFileO bj);
});
}
Result from download file
{ contentHeaders:
{ 'Content-Disposition': 'attachment; filename="e.docx"; filename*=UTF-8\'\'e.docx' ,
'Content-Type': 'application/vnd.openxmlfo rmats-offi cedocument .wordproce ssingml.do cument' },
data: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 09 24 87 82 81 01 00 00 8e 05 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... >,
filename: 'e.docx' }
Maybe , I missing a step to get the data as a JSON,,,?
function to download file from dropbox
function _downloadAttachment(fromUr l, withHeaders, asFilename, callback) {
request({
uri: fromUrl,
method: 'GET',
encoding: null, // forces the request to respond with a Buffer
headers: withHeaders
}, function (error, response, body) {
}
Thanks for your help
REST CALL to download file
function load(req, res) {
filesService.downloadFromS
.then(function (downloadedFileObj) {
console.log(downloadedFile
res.status(204).send(downl
});
}
Result from download file
{ contentHeaders:
{ 'Content-Disposition': 'attachment; filename="e.docx"; filename*=UTF-8\'\'e.docx'
'Content-Type': 'application/vnd.openxmlfo
data: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 09 24 87 82 81 01 00 00 8e 05 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... >,
filename: 'e.docx' }
Maybe , I missing a step to get the data as a JSON,,,?
function to download file from dropbox
function _downloadAttachment(fromUr
request({
uri: fromUrl,
method: 'GET',
encoding: null, // forces the request to respond with a Buffer
headers: withHeaders
}, function (error, response, body) {
}
Thanks for your help
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your response and you are correct it is not a json object. I was 'JSON,stringifying' the result
So, I am downloading the file from external source
here is the code
as you can see simplifiedResponse.data has the buffer data for the document
When I display the data using 'JSON.stringify this is what I see
{"type":"Buffer","data":[8 0,75,3,4,2 0,0,0,0,0, 0,13,61,18 4,74,194,2 09,165,130 ,144,1.... ]
Now I have to base64 encode the data and sent it to another method,,,
I cannot change the encode in the rest call. My node is version is 4.5
How do I base64 encode the data?
So, I am downloading the file from external source
here is the code
function _downloadAttachment(fromUrl, withHeaders, asFilename, callback) {
request({
uri: fromUrl,
method: 'GET',
encoding: null, // forces the request to respond with a Buffer
headers: withHeaders
}, function (error, response, body) {
if (error) {
log.info(util.format('Failed to download %s from %s.', asFilename, fromUrl));
callback(error);
} else {
var simplifiedResponse = {
contentHeaders: {
'Content-Disposition': response.headers['content-disposition'],
'Content-Type': response.headers['content-type']
},
data: body,
filename: asFilename
};
callback(null, simplifiedResponse);
}
});
}
as you can see simplifiedResponse.data has the buffer data for the document
When I display the data using 'JSON.stringify this is what I see
{"type":"Buffer","data":[8
Now I have to base64 encode the data and sent it to another method,,,
I cannot change the encode in the rest call. My node is version is 4.5
How do I base64 encode the data?
Do you have to base64 encode the stringified data or a subset of the data - do you have documentation on the endpoint you have to send this to?
ASKER
I need the data encoded. The endpoint is a simple internal one .
{
"fileName": "Perfect_format_resume.doc ",
"content": ""
}
I have tried
log.debug(JSON.stringify(s implifiedR esponse.da ta.toStrin g('base64' )));
where simplifiedResponse.data is the data from the rest call
I am not sure if that correct?. It does not seem so because I get a cryptic error when I call the next endpoint and pass it the string
Unexpected 'P'
Thanks for your help
{
"fileName": "Perfect_format_resume.doc
"content": ""
}
I have tried
log.debug(JSON.stringify(s
where simplifiedResponse.data is the data from the rest call
var simplifiedResponse = {
contentHeaders: {
'Content-Disposition': response.headers['content-disposition'],
'Content-Type': response.headers['content-type']
},
data: body,
filename: asFilename
};
I am not sure if that correct?. It does not seem so because I get a cryptic error when I call the next endpoint and pass it the string
Unexpected 'P'
Thanks for your help
I am trying to understand the base64 requirement - that does not make sense to me - not saying it is not how it should be but we usually submit data to a service as a URL string (key=value&key2=value...) or as a JSON string. Base64 is usually what we do to binary data when we need to send it through systems that might use 7bit character encoding.
Back to your data - the original string you showed us appears to have a lot of data in it - now you are showing us this
Then we have this
Can you describe your process end to end so we can build up a picture of what is happening - we have a few pieces but don't have the picture on the box to figure out where those pieces go.
Back to your data - the original string you showed us appears to have a lot of data in it - now you are showing us this
{
"fileName": "Perfect_format_resume.doc",
"content": ""
}
How does the above relate to the data you showed us before.Then we have this
var simplifiedResponse = {
contentHeaders: {
'Content-Disposition': response.headers['content-disposition'],
'Content-Type': response.headers['content-type']
},
data: body,
filename: asFilename
};
Where does this come from?Can you describe your process end to end so we can build up a picture of what is happening - we have a few pieces but don't have the picture on the box to figure out where those pieces go.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
solutions show how to get data in different formats and how to convert to base64 using an external js library...
ASKER
So, I am parsing a document from dropbox and then I have to encode the data as a base64 string and sent it to a rest api to be stored
Thanks for your help