简体   繁体   中英

how to convert array api response to json in JavaScript

I have an api which return data structured like this :

//code return response.status(200).json(startdate)

results data:

[
    "2020-01-16",
    "2020-01-18",
]

i want that this api to return result like this:

Code: 200
Content:
{
    "availableDates": [
      "2017-11-24",
      "2017-11-27"
    ],
    "status": "SUCCESS",
    "message": ""
}

this is my full code where i get the data as array

app.get('/api/getBusyDays',(request, response) =>{
    odoo.connect(function (err) {



        console.log(' OdooStartDate' + dateTimeStartUsed + 'OdooStopdate' + dateTimeEndUsed);
        var params1 = [];
        params1.push(inParams1);

        console.log(' search params '+ JSON.stringify(params1));
        odoo.execute_kw('calendar.event', 'search_read', params1, function (err, value) {
            if (err) { return console.log(err) }
            if(value) {
                if (value.length > 0) {


                    value.forEach(function(a) {
                        a.start_datetime = moment(a.start_datetime).format('YYYY-MM-DD');
                        a.stop_datetime = moment(a.stop_datetime).format('YYYY-MM-DD');
                    });

                    const startdate = [...new Set(value.map(val => val.start_datetime))];

                    startdate.sort();


                   // return response.status(200).json( value)

                    return response.status(200).json(startdate)
                }
            }

you can just create an object like this:

 let arrVal = [ "2017-11-24", "2017-11-27" ]; // return Object or your framework (Express or KOA or) response Object console.log({ "Code": 200, "Content": { "availableDates": arrVal, "status": "SUCCESS", "message": "" } })

Update :

Based on the comments on this answer, modify your response so it looks like this:

app.get('/api/getBusyDays',(request, response) =>{
    odoo.connect(function (err) {



        console.log(' OdooStartDate' + dateTimeStartUsed + 'OdooStopdate' + dateTimeEndUsed);
        var params1 = [];
        params1.push(inParams1);

        console.log(' search params '+ JSON.stringify(params1));
        odoo.execute_kw('calendar.event', 'search_read', params1, function (err, value) {
            if (err) { return console.log(err) }
            if(value) {
                if (value.length > 0) {


                    value.forEach(function(a) {
                        a.start_datetime = moment(a.start_datetime).format('YYYY-MM-DD');
                        a.stop_datetime = moment(a.stop_datetime).format('YYYY-MM-DD');
                    });

                    const startdate = [...new Set(value.map(val => val.start_datetime))];

                    startdate.sort();


                   // return response.status(200).json( value)

                    return response.status(200).json({"code": 200, "content": {"availableDates": startdate, "status": "SUCCESS", "message": ""}})
                }
            }

You can create an empty object and set properties to it and send it as response from ur node api service.

let data = [
    "2017-11-24",
    "2017-11-27"
];

let response = [];
response.data = data;
response.code = 200;
response.status = 'success';
response.message = 'Mission Successful';

return response;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM