How to filter the JSON response coming from axios request?

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:

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 :

{"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 resault like this:

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

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

..... etc

please i need some help. Thank you

1 Like

Do you want to print the actual JSON, (as in print the actual string you have there), or do you want want to convert it to JS (as in use the data)?

The newlines and spaces are just because the JSON is prettified, is in it’s like

{
  "Some": "data",
  "And": {
    "Some": {
      "More": "data"
    }
  }
}

Rather than

"{"Some":"data","And":{"Some":{"More":"data"}}}

It makes no difference if you want the data, because you just run JSON.parse to get a JS object, the prettification of the string is irrelevant

Thank you for answering my issue @DanCouper , well im building an API and i want it to be returned in JSON format so it appears whenever requested after visiting the path “./standings”

Where is the data being stringified here? I can’t find the line where it’s happening.

Edit: reason I ask is easier in an example:

Compare in the browser console (or node repl or whatever):

JSON.stringify({ a: 1, b: { c: 3 }})

And

JSON.stringify({ a: 1, b: { c: 3 }}, null, 4)

(Second one should produce something similar to your JSON string)

@DanCouper well mate im sorry but im little bit beginner can you explain more. however i compared them and the second one returns better response that the first one but how should i apply it to my code.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.