Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify() only allows one value parameter. How do I add more parameters to be stringified under one brace?

http.get(options, function(res){
            fs.appendFile('log.txt', JSON.stringify(res.headers, null, 4));
})

I have a question regarding the JSON.stringify() function.

I've learned that simply using the res.headers does not in fact output to JSON format.

At the moment, I am restricted to only being able to use one res.xxxxx method within JSON.stringify(). The piece of code in question is pasted above. How am I able to use more than one value? At the moment, I can only put in res.headers into the value parameter. I would also like to use res.statusCode and my own objects, all stringified under one brace {}.

The parameters of JSON.Stringify is as follows: JSON.stringify(value, [replacer], [space]);

like image 713
theGreenCabbage Avatar asked Mar 04 '13 15:03

theGreenCabbage


People also ask

How do you Stringify a value?

Stringify a JavaScript ObjectUse the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

What does JSON Stringify () method do?

The JSON.stringify() method converts a JavaScript 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.

Is JSON Stringify optional parameter?

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. If the value is empty or null then all properties of an object are included in a string.

What is specific properties are skipped by JSON Stringify method?

Following JS-specific properties are skipped by JSON.stringify method. Function properties (methods). Symbolic properties. Properties that store undefined.


1 Answers

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);
like image 59
mooreds Avatar answered Nov 07 '22 23:11

mooreds