Search code examples
javascriptarraysjavascript-objects

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

Solution

  • 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": ""}})
                    }
                }