8

I am trying to convert an XML String that I get from a server to JSON inside my Lambda function.
I have set up this rather simple example to simulate the XML answer that i get from the server using DynamoDB. (Currently I'm just trying to get the convertion going)

'use strict';

var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});


exports.handler = function (e, ctx, callback){    
    let table = "dsbTable";
    let bpNumber = 1337;
    var test;
    var x2js = new X2JS();
    let params = {
            TableName: table,
            Key:{
                "bpNumber": bpNumber
            },
        };
    docClient.get(params, function(err, data) {
            if (err) {
                console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
                callback(err, null);
            } else {
                console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
                console.log('test' +data.Item.getBp);
                //var jsonObj = x2js.xml_str2json(data.Item.getBp);
                //console.log(jsonObj);

                callback(null, data);
            }

    });

}  ;

getting the item works just fine and is returned like this

{
  "Item": {
    "getBp": "<message version=\"1.0\" system=\"AVS/3\"><header><client>553</client><avs3-sales-organization>7564</avs3-sales-organization><avs3-service-provider>DSD</avs3-service-provider></header><body><business-partner><salutation-code>01</salutation-code><titel-code-academic/><titel-academic/><titel-code-royal/><titel-royal/><job-titel/><last-name1>Pickle</last-name1><last-name2/><first-name>N</first-name><street/><street-suffix/><street-number/><street-number-suffix/><address-line-1>10 Waterside Way</address-line-1><address-line-2/><address-line-3/><zipcode>NN4 7XD</zipcode><country-code>GB</country-code><city>NORTHAMPTON</city><district/><region-code>NH</region-code><region-text>Northamptonshire</region-text><company1/><company2/><company3/><department/><po-box/><po-box-zipcode/><po-box-city/><po-box-country-code/><major-customer-zipcode/><address-source/><advertisement>Y</advertisement><category/><bp-number>1100000772</bp-number><bp-number-external/><bp-group>ABON</bp-group><eu-sales-tax-number/><bic-master-number/><sector/><communication><communication-type>WW</communication-type><communication-value>kate.southorn@dsbnet.co.uk</communication-value><communication-default>Y</communication-default></communication><attribute><attribute-type>ACC</attribute-type><attribute-value>Y</attribute-value></attribute><attribute><attribute-type>OIEMEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OINLIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OISMEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OISMIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOEMIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOFXEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOFXIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOPTEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOPTIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOTEEX</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>OOTEIN</attribute-type><attribute-value>N20121211</attribute-value></attribute><attribute><attribute-type>THEDSU</attribute-type><attribute-value/></attribute></business-partner></body></message>",
    "bpNumber": 1337
  }
}

My main issue now is that I can not figure out how i can import any XMLtoJSON library files like this one here

I hope my code in this case is not completely worthless and there is a rather simple solution.

Cœur
  • 37,241
  • 25
  • 195
  • 267
weggi_swa
  • 330
  • 1
  • 5
  • 12
  • 1
    `var jsonObj = x2js.xml_str2json(data.Item.getBp)` does not work? I think you're just not referencing the JS files. – Blue Eyed Behemoth Aug 26 '16 at 13:29
  • First of all, thank you for your time. yea, that is probably the reason. but I cannot figure out how reference it inside my lambda function. Im very new to the whole AWS thing. and javascript for that matter...I'll probably have to try something like this for Lambda to be able to access the external libraries http://docs.aws.amazon.com/de_de/lambda/latest/dg/nodejs-create-deployment-pkg.html. I have tried just pasting the code from the xml2json libraries inside my Lambda function but that just throws tons of errors :-/ – weggi_swa Aug 26 '16 at 13:38
  • Check out this link: http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file – Blue Eyed Behemoth Aug 26 '16 at 13:45
  • 3
    You have to package the dependencies into a zip file, and deploy that zip file to Lambda. You should be using `npm` to download the packages into your local environment, and then zipping those up with your script into a single deployment package, as described in the documentation you linked to. You might want to look into the Apex framework which makes this process much simpler. – Mark B Aug 26 '16 at 15:24

2 Answers2

11

You're going through the path that many new Lambda users have gone.

With Lambda, it is absolutely easy, you just write your code and validate that it works as expected - I mean on your computer.

Once you have validated it, do as follows:

  1. Zip the entire folder's content, including node_modules directory and any dependency that you use.
  2. Upload it to Lambda.

If you accidentally zipped the containing folder as well, that is fine, just make sure to update Lambda to run the script from: dir_name/file_name.function_name (don't forget to export function_name from your module).

mattbasta
  • 13,492
  • 9
  • 47
  • 68
johni
  • 5,342
  • 6
  • 42
  • 70
  • thank you, this worked. But of course fixing one thing, gave me the next error. Using the xml2json library I posted gave me "ReferenceError: window is not defined". Apperently this is because I was trying a clientside script on the serverside(?) I was able to get around it by using a different library, though. – weggi_swa Aug 29 '16 at 09:25
  • That's another question :). You can post it and paste the code that you've used and I'll look at it. Just paste here the link to the new post. – johni Aug 29 '16 at 09:29
  • All good. Using the other library works quite well right now. Thanks again – weggi_swa Aug 29 '16 at 09:51
0

Always the handler name is the <filename>.<handler> function name> and if the filename is incorrectly mentioned then also such error is thrown in cloudwatch logs.