Tag: json

Serializing object that contains cyclic object value

I have an object (parse tree) that contains child nodes which are references to other nodes. I’d like to serialize this object, using JSON.stringify(), but I get : TypeError: cyclic object value because of the constructs I mentioned. How could I work around this? It does not matter to me whether these references to other […]

How to read an external local JSON file in Javascript

I have saved a JSON file in my local system and created a Javascript file in order to read the JSON file and print data out, for instance. Here is the JSON file: {“resource”:”A”,”literals”:[“B”,”C”,”D”]} Lets say this is the path where it takes you to the JSON file: /Users/Documents/workspace/test.json Could anyone please help me with […]

Array.push() if does not exist?

How can I push into an array if neither values exist? Here is my array: [ { name: “tom”, text: “tasty” }, { name: “tom”, text: “tasty” }, { name: “tom”, text: “tasty” }, { name: “tom”, text: “tasty” }, { name: “tom”, text: “tasty” } ] If I tried to push again into the […]

Fetch: POST json data

I’m trying to POST a JSON object using fetch. From what I can understand, I need to attach a stringified object to the body of the request, e.g.: fetch(“/echo/json/”, { headers: { ‘Accept’: ‘application/json’, ‘Content-Type’: ‘application/json’ }, method: “POST”, body: JSON.stringify({a: 1, b: 2}) }) .then(function(res){ console.log(res) }) .catch(function(res){ console.log(res) }) When using jsfiddle’s json […]

JSON left out Infinity and NaN; JSON status in ECMAScript?

Any idea why JSON left out NaN and +/- Infinity? It puts Javascript in the strange situation where objects that would otherwise be serializable, are not, if they contain NaN or +/- infinity values. Looks like this has been cast in stone: see RFC4627 and ECMA-262 (section 24.3.2, JSON.stringify, NOTE 4, page 507 at last […]

Converting a string to JSON object

How do you make JS think that a string is JSON ? I have a function which only works if JSON object is passed to it. If I pass a string to it, with same format as JSON, it doesnt work. So I want to make that function think that the string passed to it […]

jQuery returning “parsererror” for ajax request

Been getting a “parsererror” from jquery for an Ajax request, I have tried changing the POST to a GET, returning the data in a few different ways (creating classes, etc.) but I cant seem to figure out what the problem is. My project is in MVC3 and I’m using jQuery 1.5 I have a Dropdown […]

Is JSON Hijacking still an issue in modern browsers?

I am using Backbone.js and the Tornado web server. The standard behavior for receiving collection data in Backbone is to send as a JSON Array. On the other hand, Tornado’s standard behavior is to not allow JSON Array’s due to the following vulnerability: http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx A related one is: http://haacked.com/archive/2009/06/25/json-hijacking.aspx It feels more natural for me […]

Removing duplicate objects with Underscore for Javascript

I have this kind of array: var foo = [ { “a” : “1” }, { “b” : “2” }, { “a” : “1” } ]; I’d like to filter it to have: var bar = [ { “a” : “1” }, { “b” : “2” }]; I tried using _.uniq, but I guess because […]

PHP json_encode encoding numbers as strings

I am having one problem with the PHP json_encode function. It encodes numbers as strings, e.g. array(‘id’ => 3) becomes “{ [“id”: “3”, …) When js encounters these values, it interprets them as strings and numeric operations fail on them. Does anyone know some way to prevent json_encode from encoding numbers as strings? Thank you!