Search code examples
jsonstringify

how to solve (Uncaught TypeError: Converting circular structure to JSON)


i have the following object and i am trying to convert it to json object as follows

 var feeTransactionsArray=[];

                 $(".editor #newPayTable .mainTr").each(function(){ 

                     var feeTransactions={};
                     var studentDetails={};
                     var feeCategory={};


                     studentDetails['studentAdmissionId']=id;

                     feeCategory['feeCatId']=$(this).find('.feeCatId').val();

                     feeTransactions['studentDetails']=studentDetails;

                     feeTransactions['feeCategory']=feeCategory;

                     feeTransactions['paidOn']=paidDate;

                     feeTransactions['transReceiptNo']=receciptNumber;

                     feeTransactions['amountPaid']=$(this).find('.amount').val();

                     feeTransactions['paymentMode']=mode

                     feeTransactions['amountPaid']=refrenceNumber;

                     feeTransactions['isConcessionGiven']='no';

                     feeTransactionsArray.push(feeTransactionsArray);
                 });
                 var myJSON = JSON.stringify(feeTransactionsArray);

this gives following error

actions.js:1180 Uncaught TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)

how to solve this in my case. please help me. thank you!


Solution

  • It is not possible to stringify a circular structure in JSON. Lets see a single example:

    var a = { a: undefined };
    var b = { b: a };
    a.a = b;
    

    Then, we have an Object:

    { a: { b: { a: { b : { a ... infinite recursion
    

    ... this results into an error you described.