mspiderv
mspiderv

Reputation: 579

JSON.stringify gives different results on different URLs

Tested in chrome, firefox, opera.

  1. Open url https://www.patro.cz/
  2. Open JS console and write: JSON.stringify([])
  3. The output should be: "[]"

enter image description here

The problem is ...

  1. Open url https://www.patro.cz/kategorie/male-domaci-spotrebice
  2. Open JS console and write: JSON.stringify([])
  3. The output should be: "[]" but it is ""[]""

enter image description here

When I try to re-create the original object like this: JSON.parse(JSON.stringify([])) on the first URL it is an array [] which is correct, but on the second one it is a string "[]".

My question is how to fix this. I want to get an array (correct result) on both URLs. Is this some bug in browser?

Thank you guys

Upvotes: 1

Views: 554

Answers (1)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94359

It seems like the problem was that your page is using Prototype.js 1.6, which was from 10 years ago and it messes up your page's objects.

The problem was that Prototype.js adds .toJSON to Array.prototype which is very problematic (.toJSON instructs JSON.stringify how to serialize an object instance.) Remove that method and it should work fine.

delete Array.prototype.toJSON;
JSON.stringify([]);

Get rid of that library as soon as possible.

Upvotes: 1

Related Questions