API Gateway + Lambda download CSV file - csv

I want to do a csv download link with API Gateway + Lambda.
But there is a problem that lambda always return JSON.stringify. Is there a way to resolve this?
s-function.json
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Content-disposition": "'attachment; filename=testing.csv'"
},
"responseTemplates": {
"text/csv": ""
}
}
}
handler.js
var json2csv = require('json2csv');
module.exports.handler = function(event, context, cb) {
var fields = ['car', 'price', 'color'];
var myCars = [
{
"car": "Audi",
"price": 40000,
"color": "blue"
}, {
"car": "BMW",
"price": 35000,
"color": "black"
}, {
"car": "Porsche",
"price": 60000,
"color": "green"
}
];
var csv = json2csv({ data: myCars, fields: fields });
return cb(null, csv);
};
In the downloaded csv file.
"\"car\",\"price\",\"color\"\n\"Audi\",40000,\"blue\"\n\"BMW\",35000,\"black\"\n\"Porsche\",60000,\"green\""
Updated:
I still trying but thank you at least I have direction.
By the way, I can't find API Gateway doc about $input.body.replaceAll. replaceAll is Java function?
Finally, I resolve this by below code in Api Gateway template.
$input.body.replaceAll("\\""","").replaceAll("""","").replaceAll("\\n","
")
s-function escaped double quotes.
"responseTemplates": {
"text/csv": "$input.body.replaceAll(\"\\\\\"\"\",\"\").replaceAll(\"\"\"\",\"\").replaceAll(\"\\\\n\",\"\n\")"
}
return data:
car,price,color
Audi,40000,blue
BMW,35000,black
Porsche,60000,green
The template final replaceAll is weird. CSV don't recognize \n or \r\n, but I try copy new line in IDE and pass to code. It work and it's magical.

Serverless has changed a bit since you asked this question but if you're using the lambda_proxy method of integration, you can use a handler like:
module.exports.handler = (event, context, callback) => {
const csvRows = [
'1,"blah",123',
'2,"qwe",456'
]
const result = csvRows.reduce((prev, curr) => {
return prev + '\n' + curr
})
callback(null, {
headers: {
'Content-Type': 'text/csv',
'Content-disposition': 'attachment; filename=testing.csv'
},
body: result,
statusCode: 200
})
}
Note: I've used ES6 features so you'll want to use Node 6 or higher to directly copy-paste this example.

If you can't fix it on the Lambda function, you could probably do a replaceAll() in the API Gateway mapping template. I think this could work just to replace the escaped double quotes:
$input.body.replaceAll("\\""","")
Edit: So the swagger would be (if I got the escaping right):
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Content-disposition": "'attachment; filename=testing.csv'"
},
"responseTemplates": {
"text/csv": "$input.body.replaceAll(\"\\\"\"\",\"\")"
}
}
}

Related

get json object attribute with 'dot' in name

*** Problem solved : json.stringify was the problem.. much easier to handle when its gone.
var DBName = result['Document']['SW.Blocks.GlobalDB']['AttributeList']['Name'];
I have a xml file which describes a datablock from a PLC and want to get specific values with JS.
I converted it with xml2js module, so i have a json object to work with.
{
"Document": {
"Engineering": {
"$": {
"version": "V15"
}
},
"SW.Blocks.GlobalDB": {
"$": {
"ID": "0"
},
"HeaderAuthor": "",
"HeaderFamily": "",
"HeaderName": "",
"HeaderVersion": "0.1",
"Interface": {
...
...
"Name": "datentypen",
"Number": "6",
"ParameterModified": {
"_": "2018-09-05T11:49:37.0862092Z",
"$": {
"ReadOnly": "true"
}
},
}
}
I want to print out the "Name" and the "Number", which are part of the "AttributeList".
So how to handle with the "SW.Blocks.GlobalDB"?
Getting error : "TypeError: Cannot read property 'SW' of undefined"
var fs = require('fs');
var xml2js = require('xml2js');
var xml = fs.readFileSync('datentypen.xml');
var parser = new xml2js.Parser({explicitArray: false});
parser.parseString(xml, function(err, result) {
if (err) {
console.error('xml2js.parse error: ',err);
} else {
var injson = JSON.stringify(result,null,3);
console.log(injson);
// var injson2 = JSON.parse(injson);
// var DBnummer = injson.Document.SW.Blocks.GlobalDB.AttributeList["Name","Number"];
// console.log(DBNummer);
};
});
I read a lot about this theme but didnt found a concrete answer..
When i write ["SW.Blocks.GlobalDB"], an error about [ comes around.
Can you try reading the JSON array using Key-Value pair? I had similar issues but with a different programming language.

get json value object from mongodb

I have formData node that has dynamic jsonObject value in mongodb
{
"_id": {
"$oid": "5a71fea0f36d2848ae4f8f0a"
},
"formData": {
"pages": [
{
"name": "page1",
"questions": [
{
"type": "comment",
"name": "about",
"title": "Please tell us about your main requirements "
}
]
}
]
},
"editorId": "59678e58f36d2842f777bc48",
"datetimeSubmit": "2018/01/15"
}
I write a node API to fetch the data from mongodb, it only display ids, editorI and datetimesubmit nodes, but it ignores the formData(jsondata) field.
const Model = require('./myModel');
module.exports = {
getData: function (callback) {
Model.find({}, (err, jsonObj) => {
callback(null, {
data: jsonObj
})
})
}
}
looks like the model.find() doesn't return jsonObject value?
thanks
got my own question fixed, basically, i should also define the data type as JSON in schema, otherwise, will be ignored.

Getting key and value from JSON with angular2

I am looking for best solution how to work with JSON in my angular2 app.
My JSON is:
{
"rightUpperLogoId": {
"id": 100000,
"value": ""
},
"navbarBackgroundColorIdCss": {
"id": 100001,
"value": ""
},
"backgroundColorIdCss": {
"id": 100002,
"value": ""
},
"translationIdFrom": {
"value": "90000"
},
"translationIdTo": {
"value": "90055"
}
}
This JSON is something like configuration file for UI of app. In my application, I want to get id from rightUpperLogoId, it is 100000. With this id I need to do GET task on my backend REST api and the returned value I would like to set to value. Thank you
You could leverage Rx operators like the flatMap one with Observable.forkJoin / Observable.of.
Here is a sample:
this.http.get('config.json')
.map(res => res.json())
.flatMap(config => {
return Observable.forkJoin(
Observable.of(config),
// For example for the request. You can build the
// request like you want
this.http.get(
`http://.../${config.rightUpperLogoId.id}`)
);
})
.map(res => {
let config = res[0];
let rightUpperLogoIdValue = res[1].json();
config.rightUpperLogoId.value = rightUpperLogoIdValue;
return config;
})
.subcribe(config => {
// handle the config object
});
This article could give you more hints (section "Aggregating data"):
http://restlet.com/blog/2016/04/12/interacting-efficiently-with-a-restful-service-with-angular2-and-rxjs-part-2/

Use JSON data coming from WebApi in AngularJS application

I get some data from a WebApi, the answer (below the code to get the datas) is in JSON. But I can't access this result from angularJS. The datas look like :
{
"$id": "1",
"result": [
{
"$id": "2",
"name": "Français",
"code": "FR",
"id": 1
},
{
"$id": "3",
"name": "Néerlandais",
"code": "NL",
"id": 2
},
{
"$id": "4",
"name": "English",
"code": "EN",
"id": 3
}
]
}
But I get the error below when I try to display the result :
data.result is undefined
I get the data like this :
(function () {
angular.module('myApp')
.factory('dataService', ['$q', '$http', dataService]);
function dataService($q, $http) {
return {
initFormCustomer: initFormCustomer
};
function initFormCustomer() {
return $http({
method: 'GET',
url: 'http://localhost:123456/api/forminit/customer/',
headers: {
},
transformResponse: transformCustomer,
cache: true
})
.then(sendResponseData)
.catch(sendGetCustomerError)
}
function sendResponseData(response) {
return response.data;
}
function transformCustomer(data, headersGetter) {
var transformed = angular.fromJson(data.result);
console.log(data.result[0]);
return transformed;
}
function sendGetCustomerError(response) {
return $q.reject('Error retrieving customer(s). (HTTP status: ' + response.status + ')');
}
}
}());
The controller :
(function () {
angular.module('myApp')
.controller('customerController', ['$location', '$scope', 'dataService', CustomerController]);
function CustomerController($location, $scope, dataService) {
var vm = this;
vm.languages = dataService.initFormCustomer();
}
}());
I think the transform function gets a json string that you have to deserialize before using it as an object... try sth like:
function transformCustomer(data, headersGetter) {
var transformed = angular.fromJson(data);
console.log(transformed.result[0]);
return transformed.result;
}
Additionally you may look at the docs https://docs.angularjs.org/api/ng/service/$http . There is some code showing how to append a transform to the default one (that do the deserialization and XSRF checks)

I try to generate output the json string return from php but i cant do it

tthe following is my json string return from php and suggest me how i can get value from following json string.
[
{
"picid": "13",
"itemcode": "P-0000001",
"filename": "-1970-01-011.jpg",
"primarystatus": "active"
},
{
"picid": "16",
"itemcode": "P-0000001",
"filename": "dateArray1.jpg",
"primarystatus": "active"
},
{
"picid": "18",
"itemcode": "P-0000001",
"filename": "dateArray3.jpg",
"primarystatus": "active"
},
{
"picid": "19",
"itemcode": "P-0000001",
"filename": "dateArray4.jpg",
"primarystatus": "active"
}
]
//php
function returnALLPic()
{
if(isset($_POST['id']))
{
$data= $this->m_phone->getAllPictureInformation($_POST["id"]);
$ss= json_encode($data);
echo $ss;
}
}
//jquery
$.ajax({
type: 'POST',
url: 'http://localhost/tt/index.php/ad_access/c_r/returnALLPic',
data: 'id='+ei,
success: function(msg)
{
// console.log(JSONObject); // Dump all data of the Object in the console
$('#model').html(msg[0].itemcode)
}
});
You can parse json using following code.
var json = $.parseJSON(j);
$(json).each(function(i,val){
$.each(val,function(k,v){
console.log(k+" : "+ v);
});
});
You can do this in PHP as well.
Read this using
$data = file_get_contents('Your_URL')
and then use following function to parse it
$phpArray = json_decode($data);
You can do what ever you want do with that
If you need to output in php, use json_decode()
then get what you want from the loop
$sss= json_decode($data)