Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print JSON to screen for use with cut and paste

This is going to sound really ghetto, but I need to print some Javascript to the browser screen so that it can be cut and pasted into another program.

I'm using JSON.stringify() from json2.js, however, its not escaping characters such as quotes and new lines (",\n) which are actually control parts of a JSON object and need to be escaped.

For example, I'd get strings like this that cause problems when importing into the other program:

{
    string_property_01 : "He said "HI"";  // The string terminates after "He said "
}

Are there any libraries that I can use to escape all the characters that I need to escape here?

Thanks!

like image 234
Chris Dutrow Avatar asked Feb 23 '23 14:02

Chris Dutrow


1 Answers

I do the following, its a beautiful hack.

var g = {
    sampleFunc : function (data) {
        var dataAsText = JSON.stringify(data);
        var response = prompt('you can copy and paste this', dataAsText);
    }
}

// usage... 
g.sampleFunc({ id: "4", name: "John Smith" });

JavaScript prompt... gotta love it.

like image 64
Glenn Ferrie Avatar answered Mar 05 '23 19:03

Glenn Ferrie