Questions tagged [stringify]

A function that converts a JavaScript value to a JavaScript Object Notation (JSON) string.

A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has to be a "string". Convert a JavaScript object into a string with JSON.stringify().

JSON.stringify() documentation.

718 questions
274
votes
13 answers

JSON.stringify output to div in pretty print way

I JSON.stringify a json object by result = JSON.stringify(message, my_json, 2) The 2 in the argument above is supposed to pretty print the result. It does this if I do something like alert(result). However, I want to output this to the user by…
Alexis
  • 23,545
  • 19
  • 104
  • 143
191
votes
8 answers

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…
Loic Duros
  • 5,472
  • 10
  • 43
  • 56
91
votes
6 answers

Stringify (convert to JSON) a JavaScript object with circular reference

I've got a JavaScript object definition which contains a circular reference: it has a property that references the parent object. It also has functions that I don't want to be passed through to the server. How would I serialize and deserialize these…
user1012500
  • 1,547
  • 1
  • 16
  • 31
63
votes
4 answers

JSON.stringify throws RangeError: Invalid string length for huge objects

As the title implies I'm trying to stringify huge JavaScript Object with JSON.stringify in my Node.js app. The objects are - again - huge (tens of mega bytes), they don't contain any functions. I need to write the serialized objects to a file. What…
borisdiakur
  • 10,387
  • 7
  • 68
  • 100
61
votes
7 answers

how to use JSON.stringify and json_decode() properly

Im trying to pass a mulitidimensional Javascript array to another page on my site by: using JSON.stringify on the array assigning the resultant value to an input field posting that field to the second page using json_decode on the posted value then…
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
61
votes
9 answers

JSON.stringify deep objects

I need a function building a JSON valid string from any argument but : avoiding recursivity problem by not adding objects twice avoiding call stack size problem by truncating past a given depth Generally it should be able to process big objects,…
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
53
votes
4 answers

JSON.stringify returns "[object Object]" instead of the contents of the object

Here I'm creating a JavaScript object and converting it to a JSON string, but JSON.stringify returns "[object Object]" in this case, instead of displaying the contents of the object. How can I work around this problem, so that the JSON string…
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
47
votes
3 answers

Limit JSON stringification depth

When stringifying an object using JSON.stringify (or something similar) is there a way to limit the stringification depth, i.e. only go n levels deep into the object tree and ignore everything that comes after that (or better: put placeholders in…
Joachim Kurz
  • 2,875
  • 6
  • 23
  • 43
46
votes
6 answers

Serialization of RegExp

So, I was interested to find that JSON.stringify reduces a RegExp to an empty object-literal (fiddle): JSON.stringify(/^[0-9]+$/) // "{}" Is this behavior expected? I realize that a RegExp is an object with no properties to serialize. That said,…
anon
44
votes
7 answers

How to stringify event object?

JSON.stringify(eventObject); gives: TypeError: Converting circular structure to JSON dojox.json.ref.toJson(eventObject); gives: TypeError: Accessing selectionEnd on an input element that cannot have a selection. Is there some library/code ready to…
Tar
  • 8,529
  • 9
  • 56
  • 127
43
votes
7 answers

JSON stringify ES6 class property with getter/setter

I have a JavaScript ES6 class that has a property set with set and accessed with get functions. It is also a constructor parameter so the class can be instantiated with said property. class MyClass { constructor(property) { this.property =…
Thomas Chia
  • 455
  • 1
  • 4
  • 9
43
votes
3 answers

PHP array stringify

In a lyrics application I'm coding, I'm using an array to print an artists table. The artists array looks like this: $artists = [ [ "Avril Lavigne" ], [ "3 Doors Down" ], [ "Celine Dion" ], …
akinuri
  • 10,690
  • 10
  • 65
  • 102
40
votes
4 answers

Hide null values in output from JSON.stringify()

In my code, all of the info from a Postgres table row are stringified when a specific rowID is selected. var jsonRes = result.message.rows; document.getElementById('panel').innerHTML = '
' + JSON.stringify(jsonRes[0], null, "\t") +…
iskandarblue
  • 7,208
  • 15
  • 60
  • 130
38
votes
8 answers

How to check if a variable is a typed array in javascript?

I'm working on a game and we make extensive use of typed arrays (Float32Arrays) for our math types. We save and load the gamestate from JSON. An example of JSON stringify output is for such an array (in Chrome) is:…
AGD
  • 445
  • 1
  • 5
  • 8
35
votes
2 answers

Convert javascript object or array to json for ajax data

So I'm creating an array with element information. I loop through all elements and save the index. For some reason I cannot convert this array to a json object! This is my array loop: var display = Array(); $('.thread_child').each(function(index,…
Christine Wilson
  • 569
  • 2
  • 7
  • 14
1
2 3
47 48