-1

I would to navigate inside json to parsing json'data'. I tried to use JSON Stringfy but nothing. Another i would to navigate inside data to parse 'hometeam' for example. Thank you for your eventually help

var TelegramBot = require ('node-telegram-bot-api');
var token = '********';
var bot = new TelegramBot(token, {polling:true});
var request = require('request');
const { parse } = require('path');

bot.on("polling_error", (err) => console.log(err));
bot.onText(/\/start/, function(msg, match) {

        var chatId = msg.chat.id;
    request('https://www.oddsmath.com/api/v1/dropping-odds.json/?language=en&timezone=Europe%2FRome&provider_id=32&country_code=IT&cat_id=0&interval=60&sortBy=1&time=24&limit=20', function(error,response,body){
       if(!error && response.statusCode == 200){
           bot.sendMessage(chatId, 'Looking for...', {parse_mode:'Markdown'})
           .then(function(msg){
               var res = JSON.parse(body);
               var dat = res.data;

               console.log(dat);
               bot.sendMessage(chatId, 'Result:\n' + dat)
           })

       }
    });
});

The JSON I am parsing looks like this

{
  "3207031-0": {
    "time": '2020-06-16 12:30:00',
    "hometeam": "Regar-TadAZ Tursunzoda",     
    "awayteam": "FK Istiklol",     
    "league": "Tajikistan - National Football League"
  },
  {...}
}
Pelle
  • 6,423
  • 4
  • 33
  • 50
phil
  • 1
  • 3
  • Hey @phil its unclear what you want to achieve.Will you please rephrase the question? – M A Salman Jun 16 '20 at 11:07
  • Data from `res.data` seems to have proper json structure. What is your question? If you want to loop trough that response try to use `Object.keys(dat)` – Rafał Figura Jun 16 '20 at 11:10
  • Hi @Supercool. I want for example get `hometeam` text from first element of `res.data` – phil Jun 16 '20 at 11:17
  • Are you able to see log in your console @phil? If please add the response to the question – M A Salman Jun 16 '20 at 11:31
  • This is log, but not is all @Supercool `{ '3207031-0': { time: '2020-06-16 12:30:00', hometeam: 'Regar-TadAZ Tursunzoda', awayteam: 'FK Istiklol', league: 'Tajikistan - National Football League',` – phil Jun 16 '20 at 11:51

1 Answers1

1

To access the hometext value of first entry inside res.data

Try something like this

const hometeam = Object.values(res.data)[0].hometeam

let res = {
  data: {
    '3207031-0': {
      time: '2020-06-16 12:30:00',
      hometeam: 'Regar-TadAZ Tursunzoda',
      awayteam: 'FK Istiklol',
      league: 'Tajikistan - National Football League'
    }
  }
}
const hometeam=Object.values(res.data)[0].hometeam
console.log(hometeam)
M A Salman
  • 3,666
  • 2
  • 12
  • 26