Home > Mobile >  convert complex javascript object to JSON nodejs
convert complex javascript object to JSON nodejs

Time:10-16

I am doing a web scraping task & I want to create a JSON object from that data. This is what I attempt to do to generate my JSON.

var submitButton = driver.findElement(By.className('btn btn-primary'));

submitButton.click().then(function () {
    setTimeout(async function () {

        const pagesource = await driver.getPageSource();
        const $ = cheerio.load(pagesource);
        const tableCount = $('.table , .table-bordered').length;
        const tablesJsonArray = [];


        for (let i = 0; i < tableCount; i  ) {

            const subjectsJsonArray = [];

            const tableData = $('.table , .table-bordered').eq(i); // HTML table (Academic Year 1/2/3)
            const subjectCount = tableData.children('tbody').children('tr').length;

            for (let j = 0; j < subjectCount; j  ) {

                const subjectData = tableData.children('tbody').children('tr').eq(j); // table row

                const subjectName = subjectData.children('td').eq(0).text();
                const year = subjectData.children('td').eq(1).text();
                const credits = subjectData.children('td').eq(2).text();
                const sOrder = subjectData.children('td').eq(3).text();
                const result = subjectData.children('td').eq(4).text();
                const onlineAssignmentResult = subjectData.children('td').eq(5).text();

                const subjectDataObj = {
                    subject_name: subjectName.trim(),
                    year: year,
                    credits: credits,
                    s_order: sOrder,
                    result: result,
                    online_assignment_result: onlineAssignmentResult.trim(),
                };

                const subjectJsonString = JSON.stringify(subjectDataObj);

                const subjectJSON = JSON.parse(subjectJsonString);

                subjectsJsonArray.push(subjectJSON);

            } 

            const resultObj = {
                table: i,
                data: subjectsJsonArray
            };

            const resultJSON = JSON.parse(JSON.stringify(resultObj));

            tablesJsonArray.push(resultJSON);

        }

        console.log(tablesJsonArray);

    }, 3000);
});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

When I run this code the console output as below,

[
  {
    table: 0,
    data: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  {
    table: 1,
    data: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ]
  },
  {
    table: 2,
    data: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  }
]
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The 'subjectsJsonArray' in 'resultObj' object does not convert to JSON & only shows as [Object]. What is the proper way to create a valid JSON from 'resultObj'(with having a nested object)?

Below is the valid result JSON I need (for this example only 3 object shows inside 'data' ),

[
    {
        "table": "0",
        "data": [
            {
                "subject_name": "IT1105 Information Systems & Technology",
                "year": "[2017]",
                "credits": "3",
                "s_order": "[1]",
                "result": "B-",
                "online_assignment_result": "P"
            },
            {
                "subject_name": "IT1205 Computer Systems I",
                "year": "[2017]",
                "credits": "3",
                "s_order": "[2]",
                "result": "C",
                "online_assignment_result": "P"
            },
            {
                "subject_name": "IT1305 Web Application Development I",
                "year": "[2017]",
                "credits": "3",
                "s_order": "[3]",
                "result": "B-",
                "online_assignment_result": "P"
            }
        ]
    },
    {
        "table": "1",
        "data": [
            {
                "subject_name": "IT3105 Object Oriented Analysis & Design",
                "year": "[2018]",
                "credits": "3",
                "s_order": "[1]",
                "result": "C ",
                "online_assignment_result": "P"
            },
            {
                "subject_name": "IT3205 Fundamentals of Software Engineering",
                "year": "[2018]",
                "credits": "3",
                "s_order": "[2]",
                "result": "A-",
                "online_assignment_result": "P"
            },
            {
                "subject_name": "IT3305 Mathematics for Computing II",
                "year": "[2018]",
                "credits": "3",
                "s_order": "[3]",
                "result": "C",
                "online_assignment_result": "P"
            }
        ]
    },
    {
        "table": "2",
        "data": [
            {
                "subject_name": "IT5105 Professional Issues in IT",
                "year": "[2019]",
                "credits": "3",
                "s_order": "[0]",
                "result": "B",
                "online_assignment_result": "-"
            },
            {
                "subject_name": "IT5405 Fundamentals of Multimedia",
                "year": "[2019]",
                "credits": "3",
                "s_order": "[0]",
                "result": "B ",
                "online_assignment_result": "-"
            },
            {
                "subject_name": "IT6205 Systems & Network Administration",
                "year": "[2019]",
                "credits": "3",
                "s_order": "[0]",
                "result": "C",
                "online_assignment_result": "-"
            }
        ]
    }
]
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Appreciate your help with this as a newbie. thanks!

CodePudding user response:

Just remove these two lines:

const subjectJsonString = JSON.stringify(subjectDataObj);

const subjectJSON = JSON.parse(subjectJsonString);

And edit this line: From subjectsJsonArray.push(subjectJSON); To subjectsJsonArray.push(subjectDataObj);

CodePudding user response:

You can try this code. Here 'obj' is your nested object.

const newObj = JSON.stringify(obj, " ", 2);
console.log(newObj);

Here you can change space by changing third parameter in stringify().

  • Related