If you have an object containing multiple answers, it should be an array or map of answers.

Let's think of your object's initial state as this:

var myJson = {student: 'Student Name', answers: []};

So then you could start filling the answers array like:

myJson.answers.push({question: 'q', answer: 'a', time: 1, number_tentatives: 1});

If you'd now access myJson.answers it would be an array with one answer in it.

If you still think the way to go would be objects (so a 'key' is assigned to each answer), you would do this, instead of push:

myJson.answers['answer1'] = {question: 'q', answer: 'a', time: 1, number_tentatives: 1};

Answer from Alex Szabo on Stack Overflow
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ data types
JSON methods, toJSON
The third argument of JSON.stringify(value, replacer, space) is the number of spaces to use for pretty formatting. Previously, all stringified objects had no indents and extra spaces. Thatโ€™s fine if we want to send an object over a network. The space argument is used exclusively for a nice output. Here space = 2 tells JavaScript to show nested objects on multiple lines, with indentation of 2 spaces inside an object:
Discussions

json - Stringify JavaScript object - Stack Overflow
And I iterate on each element in the object. Thank you for your help. The answer of @Giovanni help me to found the solution. var data = {}; //values.... var objVal = {}; //other values.... var final = {}; var index = 1; for(var key in data) { final[index] = data[key]; index = index + 1; } final[index] = objVal; JSON.stringify... More on stackoverflow.com
๐ŸŒ stackoverflow.com
JSON stringify objects with json strings already as values
Assuming you don't know which properties are JSON, you could use the replacer function parameter on JSON.stringify to check if a value is a JSON string. The below example tries to parse each string inside a try..catch , so is not the most efficient, but should do the trick (on nested properties ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
json - Javascript: stringify object (including members of type function) - Stack Overflow
JSONfn plugin is exactly what you're looking for. ... I have tested with my object that has function but it isn't working. 2021-02-03T20:58:23.55Z+00:00 ... Save this answer. ... Show activity on this post. Perfectly stringify functions in objects by a replacer-argument in JSON.stringify(). More on stackoverflow.com
๐ŸŒ stackoverflow.com
Using JSON.stringify on [object Object] gives "[object Object]" on React
My suggestion is to use the debugger. More on reddit.com
๐ŸŒ r/node
19
0
September 16, 2022
๐ŸŒ
Udacity
udacity.com โ€บ blog โ€บ 2021 โ€บ 04 โ€บ javascript-json-stringify.html
Converting Javascript Objects into Strings with JSON.stringify() | Udacity
September 27, 2022 - JSON stringification is the process of converting a Javascript object to a flat JSON string that can be used inside a program.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ stringify
JSON.stringify() - JavaScript - MDN Web Docs
As an array, its elements indicate the names of the properties in the object that should be included in the resulting JSON string. Only string and number values are taken into account; symbol keys are ignored. As a function, it takes two parameters: the key and the value being stringified.
๐ŸŒ
JSONLint
jsonlint.com โ€บ json-stringify
JSON Stringify - Escape JSON for Embedding | JSONLint | JSONLint
JSON Stringify converts a JSON object into an escaped string. The result can be safely embedded inside another stringโ€”in code, databases, or even nested within other JSON.
Find elsewhere
๐ŸŒ
HostingAdvice
hostingadvice.com โ€บ home โ€บ how-to โ€บ javascript "object to string" using json.stringify()
JavaScript "Object to String" Using JSON.stringify()
March 24, 2023 - JSON.stringify( { 'foo':1, 'bar':2, 'baz':{ 'quux':3 } }, null, 't'); ... We can see that each object depth will get an additional TAB indentation.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ json-stringify-example-how-to-parse-a-json-object-with-javascript
JSON Stringify Example โ€“ How to Parse a JSON Object with JS
January 5, 2021 - And just like that, you've parsed incoming JSON with fetch and used JSON.stringify() to convert a JS object literal into a JSON string.
Top answer
1 of 6
15

You can use JSON.stringify with a replacer like:

JSON.stringify({
   color: 'red',
   doSomething: function (arg) {
        alert('Do someting called with ' + arg);
   }
}, function(key, val) {
        return (typeof val === 'function') ? '' + val : val;
});
2 of 6
9

A quick and dirty way would be like this:

Object.prototype.toJSON = function() {
  var sobj = {}, i;
  for (i in this) 
    if (this.hasOwnProperty(i))
      sobj[i] = typeof this[i] == 'function' ?
        this[i].toString() : this[i];

 return sobj;

};

Obviously this will affect the serialization of every object in your code, and could trip up niave code using unfiltered for in loops. The "proper" way would be to write a recursive function that would add the toJSON function on all the descendent members of any given object, dealing with circular references and such. However, assuming single threaded Javascript (no Web Workers), this method should work and not produce any unintended side effects.

A similar function must be added to Array's prototype to override Object's by returning an array and not an object. Another option would be attaching a single one and let it selectively return an array or an object depending on the objects' own nature but it would probably be slower.

function JSONstringifyWithFuncs(obj) {
  Object.prototype.toJSON = function() {
    var sobj = {}, i;
    for (i in this) 
      if (this.hasOwnProperty(i))
        sobj[i] = typeof this[i] == 'function' ?
          this[i].toString() : this[i];

    return sobj;
  };
  Array.prototype.toJSON = function() {
      var sarr = [], i;
      for (i = 0 ; i < this.length; i++) 
          sarr.push(typeof this[i] == 'function' ? this[i].toString() : this[i]);

      return sarr;
  };

  var str = JSON.stringify(obj);

  delete Object.prototype.toJSON;
  delete Array.prototype.toJSON;

  return str;
}

http://jsbin.com/yerumateno/2/edit

๐ŸŒ
ZetCode
zetcode.com โ€บ javascript โ€บ json-stringify
JavaScript JSON.stringify - Converting Objects to JSON
In the example, we retrieve data with the fetch function inside a browser. We prettify the output with JSON.stringify. ... In this article we have converted JavaScript objects into JSON strings with the JSON.stringify function.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ wqoreoyp โ€บ javascript-json-stringify-example
How to stringify a JavaScript object to JSON string?
January 31, 2023 - The JSON.stringify(value, replacer, space) method converts JavaScript objects to a JSON string. The resulting JSON string is a JSON-formatted or serialized object that can be sent over the network or stored on a disk.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-json-stringify-method
JavaScript JSON stringify() Method - GeeksforGeeks
July 11, 2025 - The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_stringify.asp
JavaScript JSON stringify() Method
The JSON.stringify() method converts JavaScript objects into strings.
๐ŸŒ
Reddit
reddit.com โ€บ r/node โ€บ using json.stringify on [object object] gives "[object object]" on react
r/node on Reddit: Using JSON.stringify on [object Object] gives "[object Object]" on React
September 16, 2022 -

I am trying to pass an object data from one page to another.

I have a line of code that can pass id and I tried to use it to pass object rather than an integer id but I can't get it right.

The code for passing id from source page:

const navigate = useNavigate();
id && navigate(generatePath("/employeelistedit/:id", { id })); //sample code which works fine when used

The code for passing the object data from source page copying the format of the code above:

function sourcePage(){
const navigate = useNavigate();
var values = {id: "someData", startd: "someData", endd: "someData"}
values && navigate(generatePath("/harvestcalendarmonitoring/:values", { values }));
    } //with useNavigate and generatePath

This is the code in another page which receives the data:

const { values } = useParams(); //values gives [object Object]
const x = JSON.stringify(JSON.stringify(values)) //gives "[object Object]"
const y = Object.prototype.toString.call(values) //gives [object String]

For my routing, this is how I wrote it:

<Route path="/harvestcalendarmonitoring/:values" element={< Harvestcalendarmonitoring />} /> //refers to the receiving page

I know I'm not doing it right cause I know that "[object Object]" is showing that something is wrong somewhere in my codes.

Any help and suggestions would be really much appreciated. Thank you in advance.

๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_stringify.asp
JavaScript JSON.stringify()
The JSON.stringify() function will convert Date objects into strings.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ json-stringify-method-explained
JSON Object Examples: Stringify and Parse Methods Explained
February 16, 2020 - //JSON-unsafe values passed as properties on a object var obj = { x: undefined, y: Object, z: Symbol('') }; //JSON.stringify(obj); logs '{}' obj.toJSON = function(){ return { x:"undefined", y: "Function", z:"Symbol" } } JSON.stringify(obj); //"{"x":"undefined","y":"Function","z":"Symbol"}" //JSON-unsafe object with circular reference on it var o = { }, a = { b: 42, c: o, d: function(){} }; // create a circular reference inside `a` o.e = a; // would throw an error on the circular reference // JSON.stringify( a ); // define a custom JSON value serialization a.toJSON = function() { // only include the `b` property for serialization return { b: this.b }; }; JSON.stringify( a ); // "{"b":42}"
๐ŸŒ
Medium
medium.com โ€บ @LearnITbyPrashant โ€บ json-stringify-or-stringify-object-jsonobject-e55661fa5d63
JSON.stringify or stringify(Object jsonObject) | by Prashant Kumar LearnIT | Medium
January 9, 2024 - Creates a string from a JSON object. The JSON.stringify() method can only convert numbers, strings, and Java native objects to strings.