Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript how to filter the json response coming from axios request?

iimadouu

New Coder
hello, im trying to filter and organize a Json response from axios.get, im requesting a soccer standings table i ve succeded in returning the data but is bit messy,
im using node
express,
axios,
and cheerio as you can see on my code below
this is my code:

JavaScript:
const express = require('express');
const PORT = 8888
const app = express();
const cheerio = require('cheerio');
const axios = require("axios");

app.get('/td', async (req, res) => {
  var results = [];
  try {
    const url = "https://www.footmercato.net/algerie/ligue-1/classement";

    const { data } = await axios.get(url);
    
    const $ = cheerio.load(data);

    $("body > div.container.container--flex.container--column > div.content > section > div.classement.classement--longMode > table > tbody").each((index, element) => {
      var team = $(element).find("td.classement__team").text();     
      var rank = $(element).find("td.classement__rank").text();     
      var points = $(element).find("td.classement__highlight").text();
      results.push({team: team.trim(),
                    rank: rank.trim(),
                    points: points.trim(),
                  });
    });

  } catch (err) {
    console.error(err);
  }
    
   res.json({
    "message":"success",
    "data": results
  })
});

app.listen(PORT, () => console.log(`surver running on PORT ${PORT}`))

and this is how the response looks like :

JSON:
{"message":"success","data":[{"team":"Belouizdad\n                                    \n                                \n                                    \n                                        CS Constantine\n                                    \n                                \n                                    \n                                        Saoura\n                                    \n                                \n                                    \n                                        MC Alger\n                                    \n                                \n                                    \n                                        ES Sétif\n                                    \n                                \n                                    \n                                        Khenchela\n                                    \n                                \n                                    \n                                        USM Alger\n                                    \n                                \n                                    \n                                        RC Arbaâ\n                                    \n                                \n                                    \n                                        ASO Chlef\n                                    \n                                \n                                    \n                                        US Biskra\n                                    \n                                \n                                    \n                                        NC Magra\n                                    \n                                \n                                    \n                                        MC Oran\n                                    \n                                \n                                    \n                                        Bayadh\n                                    \n                                \n                                    \n                                        Kabylie\n                                    \n                                \n                                    \n                                        Paradou\n                                    \n                                \n                                    \n                                        HBCL","rank":"1\n                                \n                                    2\n                                \n                                    3\n                                \n                                    4\n                                \n                                    5\n                                \n                                    6\n                                \n                                    7\n                                \n                                    8\n                                \n                                    9\n                                \n                                    10\n                                \n                                    11\n                                \n                                    12\n                                \n                                    13\n                                \n                                    14\n                                \n                                    15\n                                \n                                    16","points":"3029232222201918171717161612121"}]}


what i want is how do i remove these " /n /n /n …" that are showing up and the second thing is i want to make the result like this:

JSON:
"team": "Belouizdad"
"rank": "1"
"points: "30"

"team": "CS Constatine"
"rank": "2"
"points":"29"

..... etc

Thank you!
 
Yes it is a bit messi ⚽🏆 🤣
I don't know anything about express and cheerio, but isn't there a way to get the JSON in a more structured format ? Very inconvenient that all the 16 teams are lumped up in one string, and all the 16 ranks, and all the 16 points. Of course we can break up these e strings and create a new JSON string with a record for each team, but I notice the points are only 31 digits:

3029232222201918171717161612121

so how are you going to decide how many points each team has ? That problem needs solving first, I think...
 
Back
Top Bottom