0

Here is my mvc code, I want to add multiple travelers objects to travelers array which are generated in a loop and then JSON.stringify them,

return amadeus.booking.flightOrders.post(
        JSON.stringify({
          'data':{
            'type': 'flight-order',
            'flightOffers': [response.data.flightOffers[0]],  
           'travelers':[{
              "id": 1,
              "name": {
                "firstName": req.body.firstname,
                "lastName": req.body.lastname
              },
              "gender": req.body.gender,
              "contact": {
                "emailAddress": req.body.emailaddress,
                "phones": [{
                  "deviceType": req.body.devicetype,
                  "countryCallingCode": req.body.countrycallingcode,
                  "number": req.body.number
                }]
              },
              "documents": [{
                "documentType": req.body.documentype,
                "birthPlace": req.body.birthplace,
                "issuanceLocation": req.body.issuancelocation,
                "issuanceDate": req.body.issuancedate,
                "number": req.body.p_number,
                "expiryDate": req.body.expirydate,
                "issuanceCountry": req.body.issuancecountry,
                "validityCountry": req.body.validitycountry,
                "nationality": req.body.nationality,
                "holder": true
              }]
            }]
        } 
      })
      );

I there a simple way to achieve that?

5
  • JSON.stringify is a function. it expects a value to be passed to it. There's nothing stopping you from building that value outside of the JSON.stringify call's argument list. Commented Oct 7, 2020 at 14:30
  • You can add multiple travelers to the travelers array and stringify it, it has nothing to do with JSON.stringify Commented Oct 7, 2020 at 14:33
  • @MetaPakistani could you guide, me how can i do it? Commented Oct 7, 2020 at 14:39
  • @aghahamza Check explaination in answer Commented Oct 7, 2020 at 14:47
  • @MetaPakistani is there any way to insert travelers through loop? Commented Oct 7, 2020 at 14:52

1 Answer 1

1

JSON.stringify converts a JS object to a string. It does not control or require any specifications for the object's structure in general.

In your case, you can simply add multiple data objects to your travelers' array e.g

return amadeus.booking.flightOrders.post(
    JSON.stringify({
      'data':{
        'type': 'flight-order',
        'flightOffers': [response.data.flightOffers[0]],  
       'travelers':[{
          "id": 1,
          "name": {
            "firstName": req.body.firstname,
            "lastName": req.body.lastname
          },
          "gender": req.body.gender,
          "contact": {
            "emailAddress": req.body.emailaddress,
            "phones": [{
              "deviceType": req.body.devicetype,
              "countryCallingCode": req.body.countrycallingcode,
              "number": req.body.number
            }]
          },
          "documents": [{
            "documentType": req.body.documentype,
            "birthPlace": req.body.birthplace,
            "issuanceLocation": req.body.issuancelocation,
            "issuanceDate": req.body.issuancedate,
            "number": req.body.p_number,
            "expiryDate": req.body.expirydate,
            "issuanceCountry": req.body.issuancecountry,
            "validityCountry": req.body.validitycountry,
            "nationality": req.body.nationality,
            "holder": true
          }]
        },{
          "id": 2,
          "name": {
            "firstName": req.body.firstname,
            "lastName": req.body.lastname
          },
          "gender": req.body.gender,
          "contact": {
            "emailAddress": req.body.emailaddress,
            "phones": [{
              "deviceType": req.body.devicetype,
              "countryCallingCode": req.body.countrycallingcode,
              "number": req.body.number
            }]
          },
          "documents": [{
            "documentType": req.body.documentype,
            "birthPlace": req.body.birthplace,
            "issuanceLocation": req.body.issuancelocation,
            "issuanceDate": req.body.issuancedate,
            "number": req.body.p_number,
            "expiryDate": req.body.expirydate,
            "issuanceCountry": req.body.issuancecountry,
            "validityCountry": req.body.validitycountry,
            "nationality": req.body.nationality,
            "holder": true
          }]
        },{
          "id": 3,
           .
           .
           .
        },{
          "id": 4,
           .
           .
           .
        }]
      } 
    })
  );

and JSON.stringify will convert whole object to string.

To loop on data and add the travelers array, one approach will be:

const travelers = [];
for(let i=0;i<10;i++){

   travelers.push({});
} 

and then

return amadeus.booking.flightOrders.post(
    JSON.stringify({
      'data':{
        'type': 'flight-order',
        'flightOffers': [response.data.flightOffers[0]],  
       'travelers': travelers
      } 
    })
  );
Sign up to request clarification or add additional context in comments.

1 Comment

is there any way to insert travelers through loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.