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.

636 questions
202
votes
12 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
  • 19,151
  • 17
  • 91
  • 134
159
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
  • 4,672
  • 8
  • 36
  • 52
81
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,161
  • 1
  • 11
  • 23
55
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
  • 335,116
  • 73
  • 720
  • 697
54
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
  • 17,890
  • 15
  • 70
  • 121
42
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
  • 25,996
  • 59
  • 164
  • 297
41
votes
1 answer

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
  • 8,405
  • 7
  • 58
  • 92
37
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
37
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
  • 7,277
  • 7
  • 45
  • 101
34
votes
6 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
  • 365
  • 1
  • 3
  • 9
34
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
  • 527
  • 2
  • 5
  • 12
34
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,575
  • 5
  • 21
  • 40
30
votes
1 answer

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
  • 7,673
  • 10
  • 47
  • 80
28
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") +…
the_darkside
  • 5,688
  • 7
  • 36
  • 83
24
votes
9 answers

Json - stringify so that arrays are on one line

Is it possible to stringify a JSON object to look like this, with arrays in one line - not indented { "Repeat": { "Name": [["Top_level","All"],[[1,1]]], "Link": [["Top_level"],[[1,1]]] }, "Delete": ["Confirm","Cancel"], …
Chris Glasier
  • 683
  • 2
  • 9
  • 20
1
2 3
42 43