3
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);

3
  • What browser do you use ? And do you use a library for JSON? Commented Nov 12, 2011 at 18:38
  • I am using Chrome, i am not using any library for JSON. Commented Nov 12, 2011 at 18:41
  • can you share sample code you have written? Commented Nov 12, 2011 at 19:07

2 Answers 2

12

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).

Sign up to request clarification or add additional context in comments.

1 Comment

-1

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

debug

4 Comments

Trying this in chrome. My array contains objects of product.
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.
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);
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.