I want to directly convert a javascript object to string. I used following code.

var foo = {};  
foo.test1 = test1;  
foo.test2 = test2;  
foo.test3 = test3;  

var jsonObj = JSON.stringify(foo); 

It works fine but it uses the json2 javascript library. However I need to do this in plain javascript without using any libraries. I know creating the json feed using passed parameters will work like this.

var jsonObj = "{\"test1\":\"" + test1+ "\",\"test2\":\"" + test2+ "\",\"test3\":\"" + test3+ "\"}";

However if the passed parameters(test1, test2 and test3) contains double quotes it will have issues.

What is the best approach to achieve this?

Thank you

link|improve this question

68% accept rate
1  
Why can't you use a library? Especially one as small and special purposed as json2.js. – Quentin Jan 17 at 9:31
1  
Indeed, you could even stick a minified version of json2.js in along with your other code, you don't have to keep it in a separate file. – Andy E Jan 17 at 9:37
You don't need to include json2.js if you aren't planning on supporting very old browsers (or browsers built like its still 2001). – techfoobar Jan 17 at 9:52
1  
I dont want to use this im dealing with a server side javascript library like node.js. Hence its essential to omit this kind of third party libraries. – Dilshan Jan 17 at 10:05
1  
@Dilshan: if it's server-side JS, there's a good chance the implementation supports JSON.stringify natively. node.js does. – Andy E Jan 17 at 10:11
show 1 more comment
feedback

2 Answers

up vote 2 down vote accepted

You should escape the double quotes by performing a String.replace(/"/g, "\\\"") on each key and member. For this to work however, you need to guarantee that you will only have simple strings/ numbers in your JS object.

FYI, it should be noted that the json2 library will only be used when a native implementation of JSON does not exist; all modern browsers have JSON support build in (IE < 8 is the noticable exception).

link|improve this answer
1  
Will fail if the string includes a new line – Quentin Jan 17 at 9:34
2  
Why are you escaping the double quotes in the regular expression? They don't have any special meaning there so that just adds line noise. – Quentin Jan 17 at 9:35
1  
@Quentin: Because it was a string before and I forgot to remove it when I changed it to a regular expression ;) – Matt Jan 17 at 9:37
Thanks for your reply – Dilshan Jan 17 at 10:04
Yes replacing this is fine. But I needed a better way of doing this. Isnt there any straight forward approach to convert directly like that json2 library function? I tried to use JSON.stringifyin my server side javascript and simply it failed. Im using latest firefox version. – Dilshan Jan 17 at 10:14
feedback

I think you've totally misunderstood what JSON is. JSON Stands for Javascript Object Notation.

What you haven't realised is that foo is already an object and further have you actually thought about what you'd be coding to access jsonObj ?

Here's a hint jsonObj.test1 : looks familiar doesn't it.

What you might be trying to do is to create a string that looks like JSON content but isn't in fact an object. That's a different question though.

Hope this helps.

link|improve this answer
Yes Im trying to create a string like json – Dilshan Jan 17 at 10:03
1  
so can I assume that you will not be using jsonObj like an object? You will be just printing it to screen or something? – T9b Jan 17 at 11:30
I need to read properties – Dilshan Jan 17 at 11:33
Then you don't need to do any conversion at all! A string and an object are not the same thing. If you read my answer you would have realsed that foo === jsonObj you do not need to even create jsonObj because you can read properties directly from foo. But if you really must, then all you needed to do was var jsonObj = foo; then you could happily read the same properties without all that string nonsense. – T9b Jan 17 at 14:51
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.