Question on Javascript response handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Black
    Junior Member
    • Nov 2014
    • 2

    #1

    Question on Javascript response handling

    I recently started to work with Betfair's API-NG using Javascript and I am stuck at what is probably a real beginner issue. I am using Algotrader's library, which is great, and I am able to do most of the things that are of my interest. However, I am having trouble with handling the responses of multidimensional objects. I have the following code:

    Code:
    function listMarketBook(callback){ 
    	console.log("Get MarketBook response");
    	var filter = 	{"marketIds":["1.116312560"],"priceProjection":{"priceData":["EX_BEST_OFFERS"]},"marketProjection": "RUNNER_DESCRIPTION"}
    
    var invocation = session.listMarketBook(filter, function(err,res) {
         	if(err) {
            	 console.log('Getting MarketBook response failed');
            	 console.log(err);
            	 console.log(invocation);
    			 } else {
    			           console.log(invocation.request);
    				   console.log(invocation.response.result);
                             }		 					 		 
    	})
    };
    The response I get is:

    Code:
    MarketBook response
    { jsonrpc: '2.0',
      id: 1,
      method: 'SportsAPING/v1.0/listMarketBook',
      params: 
       { marketIds: [ '1.116312560' ],
         priceProjection: { priceData: [Object] },
         marketProjection: 'RUNNER_DESCRIPTION' } }
    [ { marketId: '1.116312560',
        isMarketDataDelayed: false,
        status: 'OPEN',
        betDelay: 0,
        bspReconciled: false,
        complete: true,
        inplay: false,
        numberOfWinners: 1,
        numberOfRunners: 3,
        numberOfActiveRunners: 3,
        lastMatchTime: '2014-11-18T18:18:54.772Z',
        totalMatched: 160215.6,
        totalAvailable: 762737.37,
        crossMatching: true,
        runnersVoidable: false,
        version: 862639130,
        [B][COLOR="Red"]runners: [ [Object], [Object], [Object] ][/COLOR][/B] } ]
    I am now interested in getting the details of the Objects in "runners". I can do this by running the response through a for - in loop with a changed var invocation:

    Code:
    var invocation = session.listMarketBook(filter, function(err,res) {
         	if(err) {
            	 console.log('Getting MarketBook response failed');
            	 console.log(err);
            	 console.log(invocation);
    			 } else {
    				   console.log(invocation.request);
    				   for(var index in invocation.response.result) {
    					 var item = invocation.response.result[index];
    					 console.log(item);
    					 }
    				   }		 					 		 
    			})
    The response is:

    Code:
    { jsonrpc: '2.0',
      id: 1,
      method: 'SportsAPING/v1.0/listMarketBook',
      params: 
       { marketIds: [ '1.116312560' ],
         priceProjection: { priceData: [Object] },
         marketProjection: 'RUNNER_DESCRIPTION' } }
    { marketId: '1.116312560',
      isMarketDataDelayed: false,
      status: 'OPEN',
      betDelay: 0,
      bspReconciled: false,
      complete: true,
      inplay: false,
      numberOfWinners: 1,
      numberOfRunners: 3,
      numberOfActiveRunners: 3,
      lastMatchTime: '2014-11-18T18:25:04.229Z',
      totalMatched: 160220.54,
      totalAvailable: 762734.93,
      crossMatching: true,
      runnersVoidable: false,
      version: 862639130,
      runners: 
       [ { selectionId: 55190,
           handicap: 0,
           status: 'ACTIVE',
           lastPriceTraded: 1.23,
           totalMatched: 158317.68,
           ex: [B][COLOR="Red"][Object][/COLOR][/B] },
         { selectionId: 1703,
           handicap: 0,
           status: 'ACTIVE',
           lastPriceTraded: 18,
           totalMatched: 509.47,
           ex: [COLOR="red"][B][Object][/B][/COLOR] },
         { selectionId: 58805,
           handicap: 0,
           status: 'ACTIVE',
           lastPriceTraded: 7.4,
           totalMatched: 1393.38,
           ex: [COLOR="red"][Object][/COLOR] } ] }
    So this brings me to the next dimension of objects that I am interested in. Ultimately I would like to obtain the properties "size" and "price" which are two levels down of the object, which is the property of "ex:".

    I tried to just loop through level by level, but the code got very ugly (and probably not very efficient) and I wondered whether there isn't an easier way to obtain a response which shows me every property of every object, no matter on which dimension that object is.

    It would be really great, if somebody could point me in the right direction. I spent all afternoon trying to google for a solution without success and I am quite desperate now.

    Thank you very much
    B.
  • betdynamics
    Junior Member
    • Sep 2010
    • 534

    #2
    Try using the JSON.parse(text) function.

    Comment

    • Black
      Junior Member
      • Nov 2014
      • 2

      #3
      Thank you

      Thank you very much. That was the hint that I needed.
      In case somebody else struggles with this:

      Change the console log line from
      console.log(invocation.response.result);
      To
      console.log("%j", invocation.response.result);
      This will convert the object into JSON, showing you all elements of the response.

      Comment

      Working...
      X