简体   繁体   中英

Not saving Json data to file

im scraping a page, and gathering some info to save in a file, to write to the file in json im using a libraru called "write-json" avaialble in npm.

But im having some issue, i cant save all the data in the file, it stops in a certain point, example of the data im sending:

Data:

  {
      'Sat, 31 de January de 2021': [
        PM: {
          '1': '7498-25',
          '2': '2991-23',
          '3': '8552-13',
          '4': '6850-13',
          '5': '6438-10',
          '6': '2329-8',
          '7': '426-7'
        },
        PT: {
          '1': '5767-17',
          '2': '2282-21',
          '3': '5051-13',
          '4': '0646-12',
          '5': '0133-9',
          '6': '3879-20',
          '7': '160-15'
        },
        PV: {
          '1': '8888-22',
          '2': '6620-5',
          '3': '3566-17',
          '4': '9635-9',
          '5': '0679-20',
          '6': '9388-22',
          '7': '838-10'
        }
      ],
      'Sat, 30 de January de 2021': [
        FD: {
          '1': '7619-5',
          '2': '1445-12',
          '3': '1193-24',
          '4': '6477-20',
          '5': '6819-5',
          '6': '3553-14',
          '7': '409-3'
        },
        CR: {
          '1': '7911-3',
          '2': '6040-10',
          '3': '2143-11',
          '4': '1706-2',
          '5': '0251-13',
          '6': '8051-13',
          '7': '782-21'
        },
        PTN: {
          '1': '0594-24',
          '2': '7823-6',
          '3': '4314-4',
          '4': '6884-21',
          '5': '6124-6',
          '6': '5739-10',
          '7': '646-12'
        }
      ]
      
    }

What is saved in the file is:

{
  "Sat, 31 de January de 2021": [],
  "Sat, 30 de January de 2021": []
}

Here example of my code:

var jsonfile = [];
    var $ = cheerio.load(body);

                var results =  $('table');

                var banc_results = [];

                var finalResult = {};


                results.each(function( index, element ) {

                var data = $(this).find( "caption" ).text().trim();



                    var thead = $(this).find( "thead > tr > th" );

                    thead.each(function(index, element){
                       var banc = $(this).text();

                       if(banc != ""){
                           banc_results[banca] = {};
                       }
                    });

                    $(this).find('tbody tr').each(function (i, el) {
                        var $tds = $(this).find('td')
                        var counter = i +1;
                        $tds.each(function(index, element){

                            var $th = $(this).closest('table').find('th').eq($(this).index());

                            if($th.text() != ""){
                                banc_results[$th.text()][counter] = $(this).text();
                            }

                        });

                    });



                    finalResult[data].dados = banc_results;

                });


                jsonfile = finalResult;
                console.log(jsonfile);
                
                 writeJson.sync('de-data.json', jsonfile);

Data that are you trying to save is not a legal JSON object. In the first approach, you should have brackets in the array, for example, first date first element:

{
   PM: {
        '1': '7498-25',
        '2': '2991-23',
        '3': '8552-13',
        '4': '6850-13',
        '5': '6438-10',
        '6': '2329-8',
        '7': '426-7'
      }
},

And so on for other elements. Sorry, but I can not find in your code how to correct this.

As said before, you're not using a valid JSON syntax. To be correct, it should look like this:

{
   "Sat, 31 de January de 2021":{
      "PM":{
         "1":"7498-25",
         "2":"2991-23",
         "3":"8552-13",
         "4":"6850-13",
         "5":"6438-10",
         "6":"2329-8",
         "7":"426-7"
      }
   }
}

So instead of an array for banc_results, create an object

You are using array notation ( [...] ) for what should be an object (named key-value pairs).

An Array is used for a list of elements without names, whereas an Object assigns names to values.

For all of the areas where you use [ "name": "value" ] , change it to { "name": "value" } .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM