javascriptjsonfrisby.js

How to reach contents of a nested JSON response body in javascript?


I need to automate some API tests with frisby.js, and I'm stuck at accessing to some data which is nested inside JSON response body.

Below is my code

var frisby = require('c:/frisby');

frisby.create('Request available Voyages')
    .post('someURL', {
        departureVoyage: {
            from: "500",
            to: "500",
            date: '2017-01-07'}})
    .inspectJSON()
    .afterJSON(function (voyage) {
       console.log(voyage.departureVoyage.voyages);}

Below is the output of inspectJSON() function

{ type: 'voyageResponseWsDTO',
  departureVoyage:
   { date: '2017-01-07',
     voyages:
      [ { endTime: '22:30',
          quotas:
           [ { id: '8796095719239',
               limit: 66,
               name: 'PROMOTION',
               price: '34',
               priceCurrency: 'USD' },
             { id: '8796095620935',
               limit: 271,
               name: 'ECONOMY',
               price: '51',
               priceCurrency: 'USD' },
             { id: '8796095489863',
               limit: 19,
               name: 'BUSINESS',
               price: '72',
               priceCurrency: 'USD' },
             { id: '8796234721095',
               limit: 0,
               name: 'CAR PROMOTION',
               price: '84',
               priceCurrency: 'USD' },
             { id: '8796095752007',
               limit: 2,
               name: 'VIP',
               price: '800',
               priceCurrency: 'USD' } ],
          time: '20:00',
          vessel:
           { carriesVehicles: true,
             TypeCode: 'FE',
             TypeName: 'Ferry' } } ] } }

and below is the output of console.log(voyage.departureVoyage.voyages);

[ { endTime: '22:30',
    quotas: [ [Object], [Object], [Object], [Object], [Object] ],
    time: '20:00',
    vessel:
     { carriesVehicles: true,
       TypeCode: 'FE',
       TypeName: 'Ferry' } } ]

My problem is, when I try to access content of "quotas" with "console.log(voyage.departureVoyage.voyages.quotas);" I get "undefined" message. Do you know a way how can I reach data of this "quotas"? Because I need to reach properties of this "quotas", like id of one them.

Thanks


Solution

  • voyage.departureVoyage.voyages is an array, and quotas is in the first item of that array,

    So refer it like below for quotas inside it,

    voyage.departureVoyage.voyages[0].quotas