You need to create a new js object and put res.headers into it.

var obj = {};
obj.headers = res.headers;
obj.somethingelse = somethingelse;
var string = JSON.stringify(obj);
Answer from mooreds on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ stringify
JSON.stringify() - JavaScript | MDN
The space parameter may be used to control spacing in the final string. If it is a number, successive levels in the stringification will each be indented by this many space characters. If it is a string, successive levels will be indented by this string. Each level of indentation will never be longer than 10. Number values of space are clamped to 10, and string values are truncated to 10 characters. ... JSON.stringify({}); // '{}' JSON.stringify(true); // 'true' JSON.stringify("foo"); // '"foo"' JSON.stringify([1, "false", false]); // '[1,"false",false]' JSON.stringify([NaN, null, Infinity]);
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ javascript-essentials-detailed-exploration-of-json-stringify
Exploring JSON Stringify: An In-Depth Exploration
September 29, 2023 - The syntax of JSON Stringify is quite straightforward. It takes three parameters: the value to convert, a replacer function that alters the conversion, and a space value to insert white space into the output JSON string for readability purposes.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_stringify.asp
JSON.stringify()
Use the JavaScript function JSON.stringify() to convert it into a string.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ javascript json stringify() method
JavaScript JSON stringify() Method - Scaler Topics
May 4, 2023 - When the space parameter is defined as a number, it will add the specified number of spaces in between consecutive key-value pairs in the resultant JSON string. But, the maximum number of spaces allowed is 10, while the minimum allowed is 0. So anything greater than 10 is taken as 10 and anything less than1is taken as 0. Let us look at an example. ... When defined as a string, the JSON stringify method uses that string in between adjacent keys, instead of space.
๐ŸŒ
Dillion's Blog
dillionmegida.com โ€บ p โ€บ second-argument-in-json-stringify
The second argument in JSON.stringify() - Dillion's Blog
JSON.stringify(value, replacer, space) The second parameter is a function that is called for each value in the object. It can be used to transform the value before it is converted to a string.
๐ŸŒ
The Code Barbarian
thecodebarbarian.com โ€บ the-80-20-guide-to-json-stringify-in-javascript
The 80/20 Guide to JSON.stringify in JavaScript | www.thecodebarbarian.com
`JSON.stringify()` removes functions and `undefined`. JSON.stringify(obj); // '{}' The first parameter to JSON.stringify() is the object to serialize to JSON. JSON.stringify() actually takes 3 parameters, and the 3rd parameter is called spaces.
Top answer
1 of 3
21

The second parameter can be a function to perform replacement while stringifying.

A null or undefined second parameter means that you want to use standard stringification, without any customization.

From https://developer.mozilla.org/en-US/docs/Using_native_JSON#The_replacer_pa

Starting in Firefox 3.5.4, JSON.stringify() offers additional customization capabilities through the use of optional parameters. The syntax is:

jsonString = JSON.stringify(value [, replacer [, space]])

value The JavaScript object to convert into a JSON string.

replacer A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.

space A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 if it's larger than that. Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used. The replacer parameter

The replacer parameter can be either a function or an array. As a function, it takes two parameters, the key and the value being stringified. The object in which the key was found is provided as the replacer's this parameter. Initially it gets called with an empty key representing the object being stringified, and it then gets called for each property on the object or array being stringified. It should return the value that should be added to the JSON string, as follows:

If you return a Number, the string corresponding to that number is used as the value for the property when added to the JSON string. If you return a String, that string is used as the property's value when adding it to the JSON string. If you return a Boolean, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string. If you return any other object, the object is recursively stringified into the JSON string, calling the replacer function on each property, unless the object is a function, in which case nothing is added to the JSON string. If you return undefined, the property is not included in the output JSON string. Note: You cannot use the replacer function to remove values from an array. If you return undefined or a function then null is used instead.

Example

function censor(key, value) {
  if (typeof(value) == "string") {
    return undefined;
  }
  return value;
}

var foo = {foundation: "Mozilla", 
           model: "box", 
           week: 45, 
           transport: "car", 
           month: 7};
var jsonString = JSON.stringify(foo, censor);
The resulting JSON string is {"week":45,"month":7}.

If replacer is an array, the array's values indicate the names of the properties in the object that should be included in the resulting JSON string.

2 of 3
15

There is no way to pass third parameter without passing second parameter in JavaScript.
So null is a placeholder for replacer function when you need to pass space.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_stringify.asp
JavaScript JSON stringify() Method
/*replace the value of "city" to upper case:*/ var obj = { "name":"John", "age":"39", "city":"New York"}; var text = JSON.stringify(obj, function (key, value) { if (key == "city") { return value.toUpperCase(); } else { return value; } }); Try it Yourself ยป
๐ŸŒ
pawelgrzybek
pawelgrzybek.com โ€บ til-the-power-of-json-stringify-replacer-parameter
TIL โ€” The power of JSON.stringify replacer parameter | pawelgrzybek.com
JSON.stringify() takes a second optional argument that can be a recursive replacer function or an array of white-listed keys to be stringified.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-json-stringify-method
JavaScript JSON stringify() Method - GeeksforGeeks
July 11, 2025 - This method takes a JavaScript ... that object. ... replacer: It is an optional parameter. This parameter value can be an altering function or an array used as a selected filter for the stringify....
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ parse
JSON.parse() - JavaScript | MDN
// Maps are normally serialized as objects with no properties. // We can use the replacer to specify the entries to be serialized. const map = new Map([ [1, "one"], [2, "two"], [3, "three"], ]); const jsonText = JSON.stringify(map, (key, value) => value instanceof Map ?
๐ŸŒ
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}" The replacer, as mentioned earlier, is a filter which indicates which properties are to be included in the JSON string.
๐ŸŒ
RestfulAPI
restfulapi.net โ€บ home โ€บ json โ€บ json stringify()
JSON stringify()
November 3, 2023 - The JSON.stringify() function, as name suggests, converts a JavaScript value to a serialized JSON string. JSON.stringify() can optionally use a replacer function to replace values using custom logic. 1. Syntax The syntax of the JSON.stringify() ... Read more
๐ŸŒ
4D
developer.4d.com โ€บ 4d language โ€บ commands by theme โ€บ json โ€บ json stringify
JSON Stringify | 4D Docs
$vel:=JSON Stringify(120) // "120" $vh:=JSON Stringify(?20:00:00?) // "72000" seconds since midnight SET DATABASE PARAMETER(Times inside objects;Times in milliseconds) $vhms:=JSON Stringify(?20:00:00?) // "72000000" milliseconds since midnight $vd:=JSON Stringify(!28/08/2013!)
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ why-is-the-second-argument-in-json-stringify-usually-undefined
Why is the Second Argument in JSON.stringify() usually null?
March 29, 2023 - As you can see here, the only properties in the final JSON string are name and isEngineer as those are the keys we specified in the replacer array. You use null because you do not want to replace anything. When you pass null as the replacer argument, it means "no replacements", so every property in the object is stringified.
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ how to use json.stringify
How to use JSON.stringify - Tabnine
July 25, 2024 - The JSON.stringify() method is used to convert a javascript object into its JavaScript Object Notation (JSON) string representation. This can be converted directly, or filtered through a replacer that manipulates each element as it is handled. The formatting can be additionally modified using an optional parameter to define the whitespace separator between each JSON element [โ€ฆ]
๐ŸŒ
Tomekkolasa
tomekkolasa.com โ€บ how-to-use-optional-parameters-in-json-stringify-and-json-parse
How to use optional parameters in JSON.stringify and JSON.parse
October 29, 2020 - Itโ€™s commonly used to convert ... strings. Beside a value to convert, the method also accepts two optional parameters, replacer and space:...
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ fundamentals โ€บ stringify
JSON.stringify() in JavaScript - Mastering JS
The 3rd argument to JSON.stringify() is called space. This parameter should be either a string or a number, and it tells JavaScript to format the JSON in a human readable way.
๐ŸŒ
Reality Ripple
udn.realityripple.com โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ stringify
JSON.stringify() - JavaScript
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.