Link to home
Start Free TrialLog in
Avatar of chand pb
chand pbFlag for United States of America

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.openxmlformats-officedocument.wordprocessingml.document
},
"data" : {
     "type" : Buffer,
      "data" : [
              80,
              75,
                3,
                4,
               20
        ]
},
"filename" : e.docx
}
SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chand pb

ASKER

Thanks.

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
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.
Thanks.. but I tried parse but it still does not work. getting undefined



REST CALL to download file

function load(req, res) {

   filesService.downloadFromSource(req.params.source, req.body)
    .then(function (downloadedFileObj) {
    console.log(downloadedFileObj);
       res.status(204).send(downloadedFileObj);
    });
}

Result from download file

{ contentHeaders:
   { 'Content-Disposition': 'attachment; filename="e.docx"; filename*=UTF-8\'\'e.docx',
     'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' },
  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(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) {
}

Thanks for your help
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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);
    }
  });
}

Open in new window


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":[80,75,3,4,20,0,0,0,0,0,13,61,184,74,194,209,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?
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?
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(simplifiedResponse.data.toString('base64')));

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
      };

Open in new window


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
{
"fileName": "Perfect_format_resume.doc",
  "content": ""
}

Open in new window

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
      };

Open in new window

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
solutions show how to get data in different formats and how to convert to base64 using an external js library...