title: How can I emulate php's json_encode in javascript / node js using JSON.stringify?
body: <p>I'm looking for a way to mimic php's json_encode's behavior from node js.  Here's an example showing what php does with a url that is in an object that gets json_encoded:</p>
  
  <pre><code>&lt;?
  $foo['url'] = "http://example.com/note/that/the/slashes/get/backslashed";
  echo json_encode($foo);
  ?&gt;
  </code></pre>
  
  <p>generates the following output:</p>
  
  <pre><code>{"url":"http:\/\/example.com\/note\/that\/the\/slashes\/get\/backslashed"}
  </code></pre>
  
  <p>Using node.js and the JSON.stringify function here:</p>
  
  <pre><code>var foo = new Object();
  foo.url = "http://example.com/note/that/the/slashes/do/not/get/backslashed"
  console.log(JSON.stringify(foo));
  </code></pre>
  
  <p>I observe this output instead:</p>
  
  <pre><code> {"url":"http://example.com/note/that/the/slashes/do/not/get/backslashed"}
  </code></pre>
  
  <p><em>Do you know of a clean way to get JSON.stringify to behave the same way that PHP behaves?</em></p>
  
  <p>Extra information: I realize that these slashes may not be required for correct json encoding but I am sending json encoded objects to a remote server that I don't have control over and doesn't like them without the backslashes.</p>
  
  <p>More extra information:  And I tried putting in my own backslashes and then calling JSON.stringify but JSON.stringify dutifully does properly escape the backslashes so I ended up with \\/ instead of \/ which is what I wanted.</p>