2
var Products = [
    { id: 0, product: 'Sour Apple', price: 10, count: 1, product_thumb: 'resources/css/apple.png' },
    { id: 1, product: '30 dsfdf', price: 20, count: 1, product_thumb: 'resources/css/croissant.png' },
    { id: 2, product: 'Discount Coffee', price: 30, count: 1, product_thumb: 'resources/css/coffecup.png' },
    { id: 3, product: '30 Donut Combo', price: 40, count: 1, product_thumb: 'resources/css/donut.png' },
    { id: 4, product: 'Invisishield', price: 50, count: 1, product_thumb: 'resources/css/apple.png' },
    { id: 5, product: 'Pink Cupcake', price: 60, count: 1, product_thumb: 'resources/css/icecream.png' },
    { id: 6, product: 'Strawberry Cone', price: 70, count: 1, product_thumb: 'resources/css/softy.png' }
]

I am trying to encode the product array (above) to JSON string and I am getting the following error: TypeError: Converting circular structure to JSON

UPDATE (from comment):

What i am trying to do is, i declare a var product = []; and then as and when user add's product to cart i do: var productObject = { id: id, product: name, price: price, count: 1, product_thumb: img }; Once the user says done, i take the array and want to convert it to json and send it to my web service. The problem is when i do JSON.stringify it gives that error. product.push(productObject);

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Ali
  • 93
  • 1
  • 3
  • 10

2 Answers2

11

TypeError: Converting circular structure to JSON

This error occurs when you have a cycle in your object. For example :

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

If you browse obj, you have that cycle : obj->a->b->obj->...

So, JSON.stringify(obj) raises an error.

That kind of error can also occur when you include a DOM Object (window, document...), since they or their children reference them(self).

fflorent
  • 1,596
  • 9
  • 11
  • [Serializing object that contains cyclic object value](https://stackoverflow.com/a/9382383/2162226) – Gene Bo Feb 16 '21 at 23:05
-1

What browser? What environment? Just plugged your data into Chrome Inspector, works fine..

debug

Bartek
  • 15,269
  • 2
  • 58
  • 65
  • Trying this in chrome. My array contains objects of product. – Ali Nov 12 '11 at 18:40
  • var productObject = { id: id, product: name, price: price, count: 1, product_thumb: img }; product.push(productObject); This is the way i populate the object array dynamically .. the example i gave above was just to give an idea of the object as to how it looks like. – Ali Nov 12 '11 at 18:44
  • Ok, Let me explain you in a more better way. What i am trying to do is, i declare a var product = []; and then as and when user add's product to cart i do: var productObject = { id: id, product: name, price: price, count: 1, product_thumb: img }; Once the user says done, i take the array and want to convert it to json and send it to my web service. The problem is when i do JSON.stringify it gives that error. product.push(productObject); – Ali Nov 12 '11 at 19:02
  • My bad, i was not being able to explain the problem. The issue was i was using this under sencha .. all i had to do is: var arr = []; for (i = 0; i < productSelectedList.length; i++) { if (productSelectedList[i].data != null) { arr[i] = productSelectedList[i].data } } var jsonText = Ext.encode(arr); – Ali Nov 13 '11 at 05:45